Reading And Writing Binary Data


In Pro-SiMPLE, all disk files are accessed through "Channels." There are seven channels (designated by the letters A-G) available for use. To create a particular output file you must first open one of the seven channels and specify the name (or path) to the file that you wish to create. You can then begin writing output to the file. When you are finished, you should close the channel so that it knows not to expect any more data.

Binary data is written and read in almost exactly the same way that text data is written and read. (See: Writing Text Data To A File and Reading Text Data From A File.) The only difference is that, instead of using tasks such as readlinea and writelinea (which read and write an entire line of text each time they are invoked), you use tasks such as readbinarya and writebinarya to read and write only one 8-bit byte at a time.

The following example program will read the wave file tada.wav in the Windows Media folder and display a portion of it on the graphics screen:

          Pro-SiMPLE
          Int data, eof
          Call open a ("read c:\\windows\\media\\tada.wav")
          Do k = 1, 1500
             Call read binary a (data, eof)
             If (eof) quit
          Loop
          Call graphon
          Call move to (0, 240)
          Do x = 0, 640, 5
             Call read binary a (data, eof)
             If (eof) Break
             If (data>127) data=data-256
             Call line to (x, data+240)
          Loop
          Call close a
The program first invokes the system library's opena task to assign tada.wav to channel A and open it for reading. (We could have used any of the seven channels.) The program then invokes the readbinarya task in a loop (to skip the header record and stuff at the beginning of the wave file). After initializing the graphics, it then reads a segment of the sound data and plots it onto the screen. And finally, the closea task is invoked to close the channel.



  [ Webmaster | FAQ's | Home Page | Contact Us ]