Sequential files

A sequential file is an ordinary text file. Each character in the file is assumed to be either a text character or some other ASCII control character such as newline. The character is in the character set specified when the file is opened. By default this is the platform-native character set.

Sequential files provide access at the level of lines or strings of text: that is, data that is not divided into a series of records. However, a sequential file is not well suited for binary data, because a number in a sequential file is written as a character string.

Opening sequential files

A sequential file can be opened in one of three modes: input, output, or append. After opening a file, you must close it before opening it in another mode.

The syntax is:

Open fileName [For {Input | Output | Append} ] As fileNumber [Len = bufferSize] [Charset = MIMECharsetName]

Where Input means read-only access to the file, Output means write-only access, and Append means write-only access starting at the end of the file. Access in all three sequential modes is one line at a time. To get an unused fileNumber, use the FreeFile function.

bufferSize is the number of characters loaded into the internal buffer before being flushed to disk. This is a performance-enhancing feature: the larger the buffer, the faster the I/O. However, larger buffer sizes require more memory. The default buffer size for sequential files is 512 bytes.

MIMECharsetName designates the character set. The default is the platform-native character set, except that if a UTF-16 or UTF-8 byte order mark (BOM) is present, the BOM character set is used, and on OS/400® the CCSID is used if a BOM is not present.

When you try to open a file for sequential input, the file must already exist. If it doesn't, you get an error. When you try to open a nonexistent file in output or append mode, the file is created automatically.

Writing to sequential files

You can write the contents of variables to a sequential file that was opened in output or append mode using the Print # or Write # statement.

The parameters to Print can be strings or numeric expressions; they are converted to their string representations automatically.

This example writes the contents of Var1 and Var2 (separated by tabs, because of the commas in the statement) to the file numbered idFile.Print#idFile, Var1, Var2

Print #idfile, Var1, Var2

The Write # statement generates output compatible with the Input # statement by separating each pair of expressions with a comma, and inserting quotation marks around strings.

For example:

Dim supV As Variant, tailV As Variant
supV = 456 
tailV = NULL
Write #idFile, "Testing", 123, supV, tailV

The statements generate the following line in the file numbered idFile:

Note: True, False, and NULL are stored as strings "#True#", "#False#", and "#NULL#".

Reading from sequential files

To read data from a sequential file, open the file in input mode. Then use the Line Input # statement, the Input # statement, or the Input function, to read data from the file into variables.

Line Input # reads one line of text from a file, up to an end-of-line. The end-of-line is not returned in the string.

This example shows reading a file one line at a time until end-of-file. The Print statement displays the line and appends an end-of-line sequence.

Do Until EOF(idFile)
   Line Input #idFile, iLine
   Print iLine

Input # reads in data that was formatted and written with the Write # statement.

For example:

The file numbered idFile contains the line:


Then the following statements read "Testing" into liLabel, 123 into infA, 456 into supA, and the value NULL into tailV:

Dim liLabel As String, tailV As Variant
Dim infA As Integer, supA As Integer
Input #idFile, liLabel, infA, supA, tailV

If you find that you are using Write # and Input # with sequential files often, you should consider using a random file instead. Random files are better suited for record-oriented data.

The Input function reads data from a sequential file. This function takes the number of characters to read as an argument, and returns the characters. The Input$ function returns a string to the caller. The Input function returns a Variant variable.

This example reads an entire file at once into a string variable.

' LOF returns the length of the file in characters.
Dim fulFile As String
fulFile??= Input$(LOF(idFile), idFile)