Binary files
Binary files are designed to provide the most control over the organization of your data for both reading and writing. However, you must know exactly how the file was written.
Opening binary files
The syntax is:
Open fileName For Binary As fileNumber
Record-length arguments are ignored.
If the file does not exist, it is created, regardless of the access type supplied to the Open statement.
Using variable-length fields
Binary files can hold variable-length records. Since you need to know the string sizes to read them, you should assign a length field to each variable-length record (each string). This is not necessary if the string is a component of a user-defined type; in this case, LotusScript® automatically assigns one.
Binary access provides a byte-by-byte view of a file. A file appears to be a continuous stream of bytes, which may or may not be alphanumeric characters.
Writing to binary files
To write to a binary file, use this Put statement:
Put fileNumber , bytePosition , variableName
Here, the bytePosition parameter is the position in the file at which to start writing. The first byte in a file is at position 1; position zero is illegal, and results in an error.
Reading from binary files
To read data from a binary file, use the following:
- Get
The Get statement reads the correct number of bytes into any variable of known length, such as a fixed-length string or an integer. For variable-length strings, the number of characters read equals the current length of the string. This will be zero for uninitialized variable-length strings so you should first set the current length to the length of the string to be read. If the string in the file is within a user-defined type, the string length was stored by LotusScript® with the string.
- Seek
The Seek statement sets the byte position in an open file. The syntax is:
Seek [#] fileNumber, position
where fileNumber is the number assigned to the file when it was opened and position is the desired file position for the next read operation. In a binary file, this is a non-zero byte location. The record number in a Get statement or Put statement overrides a file position set by a Seek statement.
- Input
The Input function or the Input$ function also reads data from a binary file. The syntax is:
dataHold = Input ( numBytes , fileNumber )
where dataHold is a Variant. (If the Input$ function were used instead of the Input function, dataHold is a String.) This function reads numBytes bytes from the file and returns them in the variable dataHold.