Thread: File I/O
View Single Post
 
Old 11-26-2003, 08:46 PM
GavinO's Avatar
GavinO GavinO is offline
Coder of Fortune

Retired Leader
* Expert *
 
Join Date: Dec 2002
Location: Troy, NY USA
Posts: 3,120
Default

File I/O in VB: Binary & Random Files

Binary and Random files are what you might consider 'real' files. They store data in the way that 'big' applications do, at the byte level. If you look at one in a text viewer, it is a garbled mess. To have a chance at making sense of them, you at least need a hex viewer, if not the program intended to read them.

What are they?

Binary files are composed of raw bytes representing the data written to them. There is no extra information stored about what a particular piece of data is; just the data. Something written as an integer might be read as a pair of bytes later, or the first byte of the integer may be read with the preceding byte to form a new integer. This is an efficient way of storing uncompressed data, as it assumes that the program reading the file will sort out what is what and get the appropriate meaning of each byte.

Random files store data in a compact way, like Binary files do, but with more structure. They are organized into units called 'records'. A record itself has no intrinsic structure, but has a size (in bytes) that is specified by you when you open the file. If you do not specify a size, it will default to 128 bytes. When you write certain kinds of data to a Random file, a bit of information about that data is also stored. Variable-length strings, for example, have their length stored with the string so that the appropriate number of characters will be read back. Provisions are made so that user-defined types are dealt with properly. Since you
specify the record len, trying to write or read more data in a record than the record holds will give you a "Bad record length" error. If the data you put in the record is less than the record size, the rest of the bytes in the record are left unmodified. This is not an error, but those bytes have no meaning to the data you've written so are "garbage". Since the garbage (remaining) bytes in a record are not written, if the last record written to the disk is "short", the file will be slightly shorter than the record len * number of records would indicate.
Normally, a User Defined Type (UDT) with a fixed size (strings and arrays, if part of the UDT, are defined with fixed lengths and dimensions) is used with a Random file. That way all the records are the same size and every record is filled. Use the LenB function on the UDT when specifying the record len in the Open statement.
To set the record length, add a term to your Open statement:
Code:
Open <filename> For Random As <filenumber> Len <recordlength>
Reading & Writing

For both Binary and Random files, reading and writing are done with the Get and Put statements, respectively:
Code:
Get <filenumber>,<address>,<variable> Put <filenumber>,<address>,<expression>
In both types of files, the next byte or record that will be written in sequence is stored, so that if you want to write sequentially to a file you can omit the <address> argument. If you specify an address, the position will be adjusted to read and write from there. Get must read into a variable; it can't read and return a value to be evaluated as part of an expression. Put can write the result of most any operation or variable.

Sometimes it is helpful to know where in the file you are, or to move to a particular place without reading or writing anything. This is accomplished with the Seek() function and the Seek statement:
Code:
Seek(<filenumber>) Seek <filenumber>,<address>
These addresses work in the same manner as addresses above, in that they count either bytes or records from the beginning of the file, starting with 1.

Special thanks to Guru passel for technical assistance with this tutorial
__________________
-- The Gavster
Like to IRC? Try irc.randomirc.com
GavServer

Last edited by herilane; 04-07-2005 at 04:25 PM. Reason: fixed missing line breaks
Reply With Quote