The Open Statement

Sequential files and random-access files share common features. You use Open for both file types. The kind of file access you achieve with Open depends on the arguments you use in the Open statement. Open reserves a file handle, also called a channel, for reading from and writing to a file. The Open statement associates a number to the handle. When this file handle is open, the data stream can flow. The mode in which you open the file number[md]read, write, or both[md]dictates how the data stream can flow between the file and the computer.

note

A file handle, also called a channel, is a unique path to a file associated with a number from the Open statement. Once Open associates a file number to the file handle, the rest of the program uses the file number to access the file. The code never has to refer to the specific filename after the Open statement.

Here's the Open statement's format:

    Open strFileName [For Mode] [AccessRestriction] [LockType] As [#]intFileNum [Len = intRecordLength]

note

Remember that when you open a program and assign a data file to a number, the program will use that file number to access the file. Your program never again has to use the filename to get to the file.

All Open statements must include the filename and file number arguments, but the other arguments are optional. A simple call to Open with only the required parameters might look like this:

Open "aFile.Txt" As #1

This statement opens a file named aFile.Txt as file number 1. To reference this file for input or output, you reference the file number. This file has been opened in random-access mode, because the default when you omit the For Mode portion is Random. All the commands in today's lesson work with text files. This lesson uses the filename extension .txt or .dat, two commonly used extensions for text files.

Top Home