Binary Files

PowerBASIC's binary file technique, an extension to Interpretive BASIC, allows you to treat any file as a numbered sequence of bytes without regard to anything, including the following: ASCII characters, number versus string considerations, record length, carriage returns.  With the binary approach to a file problem, you read and write a file by specifying exactly which bytes to read or write.  This is similar to the services provided by Windows API functions used for reading and writing files.

Flexibility always comes at a price.  Binary files require that you do all the work to decide what goes where.  Binary may be the best option when dealing with alien files that aren't in ASCII format; for example, a file created by a spreadsheet or database product.  Of course, you will have to know the precise structure of the file before you can even attempt to break it down into numbers and strings agreeable to PowerBASIC.

Every file opened in binary mode has an associated position indicator that points to the place in the file that will be read or written to next.  Use the SEEK statement to set the position indicator, and the SEEK function to read it.

Binary files are accessed in the following way:

1.        First, OPEN the file in BINARY mode.  You need not specify whether you are reading or writing; you can do either, or both.

2.        To read the file, use SEEK to position the file pointer at the byte you want to read.  Then use GET$ to read a specified number of characters into a string variable.

3.        To write to the file, load a string variable with the information to be written.  Then use SEEK to position the point in the file to which it should be written, and use PUT$ to write the data.

4.        When finished, CLOSE the file.

 

See Also

Files

Sequential Files

Random Access Files