Article ID: 151335 - Last Review: August 17, 2005 - Revision: 2.1 Working with Binary Access Files
This article was previously published under Q151335 On This PageSUMMARY
By itself, a file consists of nothing more than a series of related bytes
located on disks. When your application accesses a file, it must make
assumptions about what the bytes are supposed to represent (integers,
strings, or other data types). Microsoft Excel Visual Basic for
Applications provides functions and statements that allow you to process
the file based on these assumptions. By processing files, your application
can create, manipulate, and store large amounts of data, access several
sets of data at once, and share data with other applications. Binary
access allows you to use files to store data however you want; there are no
assumptions made about data type or requirements for standard record
length. However, you must know precisely how the data is written to the
file in order to retrieve it correctly.
MORE INFORMATIONMicrosoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
Unlike random file access, binary file access has variable length records.
There is no wasted space in a binary accessed file. If you retrieve the
data at file location 112 as an integer, bytes 112 and 113 are retrieved
to make up an integer value, because an integer data type requires two
bytes.
It does not matter that these two bytes may be part of 4 bytes previously stored as Long. It is up to your application to keep track of the contents of the file and make sure that such actions are correct. The following is an example data type for binary file access:
Type Person
LName as String
FName as String
Age as Integer
End Type
Note that LName and FName are strings, which is a variable-length data type. Age is an integer, which is a 2-byte data type. For more information about the bytes required by data types, see the online Help topic, "Data Type Summary." Advantages of Binary Access Files
Disadvantage of Binary Access Files
Writing to Files Opened for Binary AccessBecause records with binary access can be of variable length, it is necessary to actually store information about the size of each field and record so that it can be read successfully. A good way to accomplish this is to store an integer with each string to indicate the length of the string. The following is an example of creating such a file:
Type Person
LName as String
FName as String
Age as Integer
End Type
When the WriteBinary macro is run, it will create a file called BINARY.TXT. The two records in this example take up 34 bytes (as opposed to the 44 bytes required by the same data with random access). Keep in mind that when opening this file in a text editor, such as Notepad, the file will not be readable. It is a binary file, not a text file. A trade-off in using variable-length field and binary access instead of fixed-length fields and random access is that the entire record could be written with a single function call using random-access. While binary access provides greater flexibility, it also requires more code to handle I/O operations. Reading Files Opened for Binary AccessThe Get statement reads a number of bytes equal to the bytes required for the variable that is used. When you use Get with a variable-length string, the number of bytes read from the file equals the current length of the string. To temporarily set the length of a variable-length string, you can use the STRING$ function to set the variable equal to a specific number of blanks, or spaces.The following example reads a file like the one created with the WriteBinary macro:
Type Person
LName as String
FName as String
Age as Integer
End Type
APPLIES TO
| Other Resources Other Support Sites
CommunityGet Help NowArticle Translations
|






















Back to the top