Share via


Visual Basic Concepts

Processing Files with Older File I/O Statements and Functions

Ever since the first version of Visual Basic, files have been processed using the Open statement and other related statements and functions (listed below). These mechanisms will eventually be phased out in favor of the FSO object model, but they are fully supported in Visual Basic 6.0.

If you can design your application to use database files, you will not need to provide direct file access in your application. The data control and bound controls let you read and write data to and from a database, which is much easier than using direct file-access techniques.

However, there are times when you need to read and write to files other than databases. This set of topics shows how to process files directly to create, manipulate, and store text and other data.

File Access Types

By itself, a file consists of nothing more than a series of related bytes located on a disk. When your application accesses a file, it must assume what the bytes are supposed to represent (characters, data records, integers, strings, and so on).

Depending upon what kind of data the file contains, you use the appropriate file access type. In Visual Basic, there are 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 (NL). Data is stored as ANSI characters. It is assumed that a file opened for random access is composed of a set of identical-length records. You can employ user-defined types to create records made up of numerous fields — each can have different data types. Data is stored as binary information.

Binary access allows you to use files to store data however you want. It is similar to random access, except there are no assumptions 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 More Information   To learn more about file access types, see "Using Sequential File Access," "Using Random File Access," and "Using Binary File Access."

File Access Functions and Statements

The following functions are used with all three types of file access:

Dir FileLen LOF
EOF FreeFile Seek
FileCopy GetAttr SetAttr
FileDateTime Loc  

The following table lists all of the file access statements and functions available for each of the three types of direct file access.

Statements & Functions Sequential Random Binary
Close X X X
Get   X X
Input( ) X   X
Input # X    
Line Input # X    
Open X X X
Print # X    
Put   X X
Type...End Type   X  
Write # X    

For More Information   For additional information on file access functions and statements, look up the function or statement topic in the index.