Export (0) Print
Expand All

Editing Files Opened for Sequential Access

Visual Studio .NET 2003

To edit a file, you must 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.

To read strings from files

  1. Retrieve the contents of a text file by opening it for Input.
  2. Use the LineInput, InputString, or Input functions to copy the file into program variables.

Visual Basic provides statements and functions that 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, FileNum As Integer
Do Until EOF(FileNum)
   Nextline = LineInput(FileNum)
   LinesFromFile = LinesFromFile & NextLine & Chr(13) & Chr(10)

Although the LineInput function recognizes the end of a line when it comes to the carriage return/line feed sequence, it does not include that sequence when it reads the line into the variable. If you want to retain the carriage return/line feed, your code must add it.

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

LinesFromFile = InputString(FileNum, CharCount)

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

Input(FileNum, fname)
Input(FileNum, lname)
Input(FileNum, street)
Input(FileNum, city)
Input(FileNum, state)
Input(FileNum, zip)

See Also

Sequential File Access | Opening Files for Sequential Access | File Access with Visual Basic Run-Time Functions | FileOpen Function | Random File Access | Binary File Access | Writing Strings to Sequential-Access Files

© 2014 Microsoft