Bagikan melalui


Print, PrintLine Functions

Writes display-formatted data to a sequential file.

Public Sub Print( _
   ByVal FileNumber As Integer, _
   ByVal ParamArray Output() As Object _
)
' -or-
Public Sub PrintLine( _
   ByVal FileNumber As Integer, _
   ByVal ParamArray Output() As Object _
)

Parameters

  • FileNumber
    Required. Any valid file number.

  • Output
    Optional. Zero or more comma-delimited expressions to write to a file.

    The Output argument settings are:

Setting

Description

SPC(n)

Used to insert space characters in the output, where n is the number of space characters to insert.

TAB(n)

Used to position the insertion point to an absolute column number, where n is the column number. Use TAB with no argument to position the insertion point at the beginning of the next print zone.

expression

Numeric expressions or string expressions to print.

Exceptions

Exception type

Error number

Condition

IOException

54

File mode is invalid.

IOException

52

FileNumber does not exist.

See the "Error number" column if you are upgrading Visual Basic 6.0 applications that use unstructured error handling. (You can compare the error number against the Number Property (Err Object).) However, when possible, you should consider replacing such error control with Structured Exception Handling Overview for Visual Basic.

Remarks

The Print and PrintLine functions are provided for backward compatibility and may have an impact on performance. For non-legacy applications, the My.Computer.FileSystem object provides better performance. For more information, see File Access with Visual Basic.

Print does not include a line feed at the end of a line; PrintLine, however, does include a line feed.

Data written with Print is usually read from a file with LineInput or Input.

If you omit Output for PrintLine, a blank line is printed to the file; for Print, nothing is output. Multiple expressions separated with a comma will be aligned on tab boundaries, but mixing commas and TAB may result in inconsistent results.

For Boolean data, either True or False is printed. The True and False keywords are not translated, regardless of the locale.

Date data is written to the file using the standard short date format recognized by your system. When either the date or the time component is missing or zero, only the part provided is written to the file.

Nothing is written to the file if Output data is empty. However, if Output list data is DBNull, Null is written to the file.

For Error data, the output appears as Error errorcode. The Error keyword is not translated regardless of the locale.

All data written to the file using Print is internationally aware; that is, the data is properly formatted using the appropriate decimal separator. If the user wishes to output data for use by multiple locales, then Write should be used.

Writing to a file with the Print or PrintLine functions requires Write access from the FileIOPermissionAccess enumeration. For more information, see FileIOPermissionAccess Enumeration.

Example

This example uses the Print and PrintLine functions to write data to a file.

FileOpen(1, "c:\trash.txt", OpenMode.Output)   ' Open file for output.
Print(1, "This is a test.")   ' Print text to file.
PrintLine(1)   ' Print blank line to file.
PrintLine(1, "Zone 1", TAB(), "Zone 2")   ' Print in two print zones.
PrintLine(1, "Hello", "World")   ' Separate strings with a tab.
PrintLine(1, SPC(5), "5 leading spaces ")   ' Print five leading spaces.
PrintLine(1, TAB(10), "Hello")   ' Print word at column 10.

' Assign Boolean, Date, and Error values. 
Dim aBool As Boolean 
Dim aDate As DateTime
aBool = False
aDate = DateTime.Parse("February 12, 1969")

' Dates and booleans are translated using locale settings of your system.
PrintLine(1, aBool, " is a Boolean value")
PrintLine(1, aDate, " is a date")
FileClose(1)   ' Close file.

Smart Device Developer Notes

This function is not supported.

Requirements

Namespace: Microsoft.VisualBasic

**Module:**FileSystem

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

See Also

Tasks

How to: Write Text to Files in Visual Basic

How to: Write Text to Files with a StreamWriter in Visual Basic

Reference

FileOpen Function

SPC Function

TAB Function

Write, WriteLine Functions

Other Resources

File Access with Visual Basic