Export (0) Print
Expand All

Writing Strings to Sequential-Access Files

Visual Studio .NET 2003

You can add data in the form of strings to existing files via the Print Function or in the form of numbers and string expressions through the Write Function.

To write strings to files

  1. Use the FileOpen Function to open a text file for Output or Append.
  2. Use the Print function to write the strings to the file as in the following example, which a text editor might use to copy the contents of a text box into a file:
    Print(FileNum, TheBox.Text)

Visual Basic also supports the Write function, 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, as illustrated in the following code:

Dim AnyString As String, AnyNumber, FileNum As Integer
AnyString = "AnyCharacters"
AnyNumber = 23445
Write(FileNum, AnyString, AnyNumber)

The previous 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 maintaining a database using Write and Input with sequential access, consider using FilePutObject and FileGetObject with random or binary access instead; both types of access are better suited to record-oriented data.

One of the primary differences between Print and Write is that Print formats the string, while Write does not. For this reason, Print does not include quotation marks around the string, which is formatted as the user would like to see it, while Write does.

Use a function that writes each string to a new line, such as PrintLine or WriteLine if you need strings separated by line.

See Also

Sequential File Access | Opening Files for Sequential Access | Editing Files Opened for Sequential Access | File Access with Visual Basic Run-Time Functions | Random File Access | Binary File Access | Print, PrintLine Functions | Write, WriteLine Functions | LineInput Function

© 2014 Microsoft