File Access Types and Functions
By itself, a file is nothing more than a series of related bytes of data on a disk. When your application accesses a file, it must assume whether the bytes represent characters, data records, integers, strings, and so on. You tell your application what to assume by specifying the file's access type.
The file access type you use depends on what kind of data the file contains. Visual Basic provides three types of file access:
- Sequential, for reading and writing text files in continuous blocks.
- Random, for reading and writing text or binary files structured as fixed-length records.
- Binary, for reading and writing arbitrarily structured files.
Sequential access is designed for use with plain text files. Each character in the file is assumed to represent either a text character or a text-formatting sequence, such as a newline character. Data is stored as ANSI characters.
A file opened for random access is assumed to be composed of a set of identical-length records, or sets of fields containing information. You can employ user-defined types to create records made up of numerous fields, each of which can have different data types. Data is stored as binary information.
Binary access allows you to use files to store data in any form that suits your needs, whether it is numeric, string, or both. Binary access is similar to random access except that no assumptions are made about data type or record length. However, you must know precisely how the data was written to the file to retrieve it correctly. For example, if you store a series of names and phone numbers, you need to remember that the first field (the name) is text and the second field (the phone number) is numeric.
File Access Functions
The following functions are used with sequential, random, and binary file access.
|Dir Function||EOF Function||FileCopy Function|
|FileDateTime Function||FileLen Function||FreeFile Function|
|GetAttr Function||Loc Function||LOF Function|
|Seek Function||SetAttr Function|
The following table lists all file access functions available for each of the three types of direct file access.
|Print, PrintLine Functions||X|
|Write, WriteLine Functions||X|
Note For additional information on file access functions, look up the function topic in the index.