Working with Binary Files

Unlike random access files that store data in records of fixed length, binary files are a collection of records with variable lengths. For example, the first record can contain ten bytes, the second record can have only five bytes, while the third record can have 15 bytes. This method of storing data saves a lot of disk space.

Because Visual Basic doesn't need to add additional spaces to the stored string to ensure that all the fields are of the same length (as when writing data to a random access file), there is no wasted space in binary files. It's no wonder that binary files occupy less space on disk than the two types of files discussed earlier. Just like random access files, binary files can be open for simultaneous read and write operations. However, because records in binary files are of variable length, it is more difficult to manipulate these files. In order to retrieve the data correctly, you must store information about the size of each field and record.

To work with binary files, you will use the following four statements:

■ The Get statement is used to read data.

■ The Put statement allows you to enter new data to a binary file.

■ The Loc statement returns the number of the last byte that was read (in random access files the Loc statement returns the number of a record that was last read).

■ The Seek statement moves the cursor to the appropriate position inside the file.

To quickly master the usage of the above statements, open the Immediate window and enter the instructions shown in the left column below. The purpose of this exercise is to enter your first and last name in a binary file called MyData.txt and then retrieve the information you entered.

Enter in the Immediate window

Explanation:

Open "MyData.txt" For Binary As #1

MsgBox "The last byte: LOC(1)

Open the file "MyData.txt" for binary access as file number 1.

Show the number of bytes on opening the file. (The file is currently empty.)

Assign a value to the variable fname.

Assign to the variable ln the length of string stored in the variable fname.

Enter the value of the variable ln in the binary file in the position of the next byte.

Display the position of the last byte.

Enter the contents of the variable fname in the next position.

Assign a value to the variable lname.

Enter in the Immediate window

Explanation:

MsgBox entry1 Get #1, , entry2

MsgBox entry2 Get #1, , entry3

MsgBox entry3 Get #1, , entry4

MsgBox entry4

Debug.Print entry1;entry2;entry3;entry4

7 Julitta 5 Korol

Assign to the variable ln the length of string stored in the variable lname.

Enter the value of the variable ln in the binary file in the position of the next byte.

Enter the contents of the variable lname in the next byte position.

Display the position of the last byte.

Read the value stored in the position of the first byte and assign it to the variable entry1.

Display the contents of the variable entry1.

Read the next value and assign it to the variable entry2.

Display the contents of the variable entry2.

Read the next value and store it in the variable entry3.

Display the contents of the variable entry3.

Read the next value and store it in the variable entry4.

Display the contents of the variable entry4. Print in the Immediate window all the data.

The result of the previous instruction as displayed in the Immediate window.

Close #1

Close the file.

Note: The above instructions can be found in the

EnterAndDisplay procedure on the companion CD-ROM.

When entering data to a binary file, use the following guidelines:

■ Before writing a string to a binary file, assign the length of the string to an Integer-type variable. Usually the following block of instructions can be used:

string_length = Len(variable_name) Put #1, , string_length Put #1, , variable_name

■ When reading data from binary files, first read the string length and then the string contents. To do this, use the Get statement and the String$ function:

Get #1, , string_length variable_name=String(string_length, " ") Get #1, , variable_name

0 0

Responses

Post a comment