Using Sequential File Access
Collapse the table of content
Expand the table of content

Visual Basic Concepts

Visual Studio 6.0

Using Sequential File Access

It is recommended that you use File System Objects to create text files, but this information is provided in case you need to use the older text file creation methods.

Sequential access works best when you want to process files consisting only of text, such as the files created with a typical text editor — that is, files in which data is not divided into a series of records. Sequential access may not be well suited for storing long series of numbers, because each number is stored as a character string. A four-digit number would require 4 bytes of storage instead of the 2 bytes it requires to store the same number as an integer.

Opening Files for Sequential Access

When you open a file for sequential access, you open it to perform one of the following operations:

  • Input characters from a file (Input)

  • Output characters to a file (Output)

  • Append characters to a file (Append)

To open a file for sequential access, use the following syntax for the Open statement:

Open pathname For [Input | Output | Append] As filenumber [Len = buffersize]

When you open a sequential file for Input, the file must already exist; otherwise, an error occurs. When you try to open a nonexistent file for Output or Append, however, the Open statement creates the file first and then opens it.

The optional Len argument specifies the number of characters to buffer when copying data between the file and your program.

After opening a file for an Input, Output, or Append operation, you must close it, using the Close statement, before reopening it for another type of operation.

Editing Files Opened for Sequential Access

If you want to edit a file, first read its contents to program variables, then change the variables, and finally, write the variables back to the file. The following sections discuss how to edit records opened for sequential access.

Reading Strings from Files

To retrieve the contents of a text file, open the file for sequential Input. Then use the Line Input #, Input( ), or Input # statement to copy the file into program variables.

Visual Basic provides statements and functions that will read and write sequential files one character at a time or one line at a time.

For example, the following code fragment reads a file line by line:

Dim LinesFromFile, NextLine As String

Do Until EOF(FileNum)
   Line Input #FileNum, NextLine
   LinesFromFile = LinesFromFile + NextLine + Chr(13) + Chr(10)

Although Line Input # recognizes the end of a line when it comes to the carriage return–linefeed sequence, it does not include the carriage return–linefeed when it reads the line into the variable. If you want to retain the carriage return–linefeed, your code must add it.

You can also use the Input # statement, which reads a list of numbers and/or string expressions written to the file. For example, to read in a line from a mailing list file, you might use the following statement:

Input #FileNum, name, street, city, state, zip

You can use the Input function to copy any number of characters from a file to a variable, provided the variable is large enough. For example, the following code uses Input to copy the specified number of characters to a variable:

LinesFromFile = Input(n, FileNum)

To copy an entire file to a variable, use the InputB function to copy bytes from a file to a variable. Since the InputB function returns an ANSI string, you must use the StrConv function to convert the ANSI string to a UNICODE string as follows:

LinesFromFile = StrConv(InputB(LOF(FileNum), FileNum), vbUnicode)

Writing Strings to Files

To store the contents of variables in a sequential file, open it for sequential Output or Append, and then use the Print # statement. For example, a text editor might use the following line of code to copy the contents of a text box into a file:

Print #FileNum, TheBox.Text

Visual Basic also supports the Write # statement, which writes a list of numbers and/or string expressions to a file. It automatically separates each expression with a comma and puts quotation marks around string expressions:

Dim AnyString As String, AnyNumber As Integer

AnyString = "AnyCharacters"
AnyNumber = 23445
Write #FileNum, AnyString, AnyNumber

This code segment writes two expressions to the file specified by FileNum. The first contains a string and the second contains the number 23445. Therefore, Visual Basic writes the following characters (including all punctuation) to the file:


Note   If you are using Write # and Input # with sequential access, consider using random or binary access instead, because they are better suited to record-oriented data.

For More Information   For additional information on sequential file access, see "Open Statement."

© 2016 Microsoft