Share via


Visual Basic Concepts

International File Input/Output

Locale is also an important consideration when working with file input and output in Visual Basic. Both the Print # and Write # statements can be used to work with data files, but they have distinct purposes.

The Print # statement puts data into a file as the data is displayed on the screen, in a locale-aware format. For instance, date output uses the system Short Date format, and numeric values use the system decimal separator.

The Input # statement cannot read locale-aware data in Visual Basic that has been written to a file with the Print # statement. To write locale-independent data that can be read by Visual Basic in any locale, use the Write # statement instead of the Print # statement.

Write #

Like the Print # statement, the Write # statement puts data into a file in a fixed format, which ensures that the data can be read from the file in any locale when using the Input # statement. For instance, dates are written to the file using the universal date format, and numeric values are written to the file using the period as the decimal separator. In the following code example, a date and a numeric value are written to a file with the Write # statement. The same file is reopened later, its content is read with the Input # statement, and the results are printed in the Immediate window. The Long Date information is drawn from the system locale:

Dim MyDate As Date, NewDate As Date
Dim MyNumber As Variant
   MyDate = #8/2/67#
   MyNumber = 123.45
Open "Testfile" for Output As #1
   Write #1, MyDate, MyNumber
Close #1

Open "Testfile" for Input As #1
   Input #1, MyDate, MyNumber
   NewDate = Format(Mydate, "Long Date")
Debug.Print NewDate, MyNumber
Close #1

When you run this code in an English/U.S. locale, the following output appears in the Immediate window:

Wednesday, August 02, 1967      123.45

When you run this code in a French/France locale, the following output appears in the Immediate window:

mercredi 2 août 1967            123,45

In both locales, the output is accurate — that is, the information was stored and retrieved properly using the Write # and Input # statements.

For More Information*   For background information on processing files, see "Working with Files" in "Processing Drives, Folders, and Files." See also "Print # Statement" or "Write # Statement" in the *Language Reference.