CitectVBA Programming Reference > CitectVBA Function Reference > File I/O Functions > Print #

Print #

Print # statement reads data from OutputList and writes that data to a sequential file.

The Print # statement has two parameters FileNum and OutputList. The required FileNum argument is the associated file number used in the Open statement when the file was opened. The required OutputList argument is a delimited list of expressions whose values are written to the file.

Note: The number sign hash character ( # ) preceding FileNumis not optional. This character indicates disk file access with the file referenced by the system file number that follows it. Do not confuse Print # which prints to disk, with Print which displays data on the screen.

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

Note: If you want to read the data from a file using the Input # statement, use the Write # statement instead of the Print # statement to write the data to the file. Using Write #properly delimits each separate data field, so it can be read back in using Input #. Using Write # also formats the data in a manner that will allow correct read operations in most locales.

If you omit expressionlist, the Print # statement prints a blank line in the file, but you must include the comma. Because Print # writes an image of the data to the file, you must delimit the data so it is printed correctly. If you use commas as delimiters, Print # also writes the blanks between print fields to the file.

The Print # statement usually writes Variant data to a file the same way it writes any other data type. However, there are some exceptions:

If the data being written is a Variant of VarType 0 (Empty), Print # writes nothing to the file for that data item.

If the data being written is a Variant of VarType 1 (Null), Print # writes the literal #NULL# to the file.

If the data being written is a Variant of VarType 7 (Date), Print # writes the date to the file using the Short Date format defined in the WIN.INI file. When either the date or the time component is missing or zero, Print # writes only the part provided to the file.


Print #FileNum, OutputList


An Integer or numeric expression representing any valid number in the range 1 to 511 inclusive, which is referenced by the file system to be associated with an open file.


One or more formatted numeric and/or string expressions to be written to the file using the following syntax:

[ {Spc( s ) | Tab [( n ) ] } ] [expression] [charpos]


Return Value

Input # statement returns data record by record from a file opened in Input or Binary mode. Data items in a file must appear in the same order as the variables in VarList and match variables of the same data type. If a variable is numeric and the data is not numeric, a value of zero is assigned to the variable.

Related Functions

Get # | GetAttr | Input | Line Input # | Put # | Write #


The following example writes data to a test file.

Dim I, FNum, FName	' Declare variables.
For I = 1 To 3
FNum = FreeFile ' Determine next file number.
FName = "TEST" & FNum
Open FName For Output As FNum ' Open file.
Print #1, "This is test #" & I ' Write string to file.
Print #1, "Here is another "; "line"; I
Next I
Close ' Close all files.
The following example writes data to a test file and reads it back.
Dim FileData, Msg, NL ' Declare variables.
NL = Chr(10) ' Define newline.
Open "TESTFILE" For Output As #1 ' Open to write file.
Print #1, "This is a test of the Print # statement."
Print #1, ' Print blank line to file.
Print #1, "Zone 1", "Zone 2" ' Print in two print zones.
Print #1, "With no space between" ; "." ' Print two strings together.
Close #1

Open "TESTFILE" for Input As #2 ' Open to read file.
Do While Not EOF(2)
Line Input #2, FileData ' Read a line of data.
Msg = Msg & FileData & NL ' Construct message.
MsgBox Msg
Close #2 ' Close all open files.
Kill "TESTFILE" ' Remove file from disk.