PRINT# statement


Write data to a device or sequential file.


PRINT # fNum&
PRINT # fNum&, [ExpList] [SPC(n)] [TAB(n)] [,] [;] [...]
PRINT # fNum&, array$()


The first form of the PRINT# statement (with or without a trailing comma) outputs a blank line to the file (i.e. a CR/LF only).

The second form of the PRINT# statement has the following parts, which may occur in any order and quantity, within a single PRINT# statement:


Number used in an OPEN statement to open a sequential file.  It can be any numeric expression that evaluates to the number of an open file.  Note that the Number symbol (#) preceding fNum& is not optional.


Numeric and/or string expression(s) to be written to the file.


An optional function used to insert n spaces into the printed output.  Multiple use of the SPC argument is permitted in the PRINT statement, for example, between expressions.  Values of n less than 1 are ignored.


An optional function used to tab to the nth column before printing ExpList.  Multiple use of the TAB argument is permitted in the PRINT, for example, to position arguments in columns.  Values of n less than 1 are ignored.


Character that determines the position of the next character printed.  A semicolon (;) means the next character is printed immediately after the last character; a comma (,) means the next character is printed at the start of the next print zone.  Print zones begin every 14 columns.

If the final argument of a PRINT# statement is a semicolon or comma, PRINT# will not append the (default) CR/LF byte pair to the data as it is written to the file.  For example:

PRINT #1, "Hello";
PRINT #1, " world!"

...produces the contiguous string "Hello world!" in the disk file.

If you omit all arguments, the PRINT# statement prints a blank line in the file (i.e., a CR/LF pair only), but you must include the comma after the file number.  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.  Also, remember that spacing of data displayed on a text screen using monospaced characters may not work well when the data is redisplayed in a graphical environment using proportionally spaced characters.

If you are not careful, you can waste a lot of disk space with unnecessary spaces, or worse, put fields so close together that you cannot tell them apart when they are later input with INPUT#.  For example:

PRINT #1,1,2,3


1            2            3

to file #1.  Because of the 14-column print zones between characters, superfluous spaces are sent to the file.  On the other hand:

PRINT #1,1;2;3


1 2 3

to the file, and you cannot read the separate numeric values from this record because INPUT# requires commas as delimiters.  The best way to delimit fields is to put a comma between each field, like so:

PRINT #1, 1 "," 2 "," 3

which writes:

1, 2, 3

to the file, and wastes the least possible space and is easy to read with an INPUT# statement.  The WRITE# statement delimits fields with commas automatically.

PRINT# is advantageous when writing a single number or string on each line in a file.  Use PRINT# followed by a comma but no arguments to write a blank line (carriage return/linefeed) to a file:

PRINT #1,   'writes a blank line to file #1


When PRINT# specifies an array name with empty parentheses, the entire array is written to the disk file as text strings, with each element delimited by a CR/LF ($CRLF or CHR$(13,10)).  Numeric arrays are converted to the ASCII text equivalent.


Arrays of User-Defined Types (UDTs) may not be used with the array form of the PRINT# statement.

See also



' Classic PRINT# statement example
SUB MakeFile
  ' opens a sequential file for output. Using PRINT #,
  ' it writes lines of different data types to the file.
  StringVariable$ = "I'll be back."
  IntegerVar% = 1000
  FloatingPoint! = 30000.12
  ' Write a line of text to the sequential file.
  PRINT #x&, StringVariable$
  PRINT #x&, IntegerVar%
  PRINT #x&, FloatingPoint!
  CLOSE #x&  ' close file variable
END SUB ' end procedure MakeFile

SUB ReadFile
  ' Opens a sequential file for input. Using INPUT #,
  ' reads lines of different types of data from the file.
  RESET StringVariable$
  RESET IntegerVar%
  RESET FloatingPoint!
  ' Read a line of text from the sequential file.
  INPUT #x&, StringVariable$
  INPUT #x&, IntegerVar%
  INPUT #x&, FloatingPoint!
  CLOSE #x&  ' close file variable
END SUB ' end procedure ReadFile

' Array mode PRINT# statement example
a$ = "Trevor, Bob, Bruce, Dave, Simon, Jenny"
PARSE a$, b$()
OPEN "filename.txt" FOR OUTPUT AS #1
PRINT #1, b$()