Q(uick)BASIC Statement: PRINT #

Quick View

PRINT #, PRINT # USING

File I/O statements that write data to a sequential file

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • PRINT [#filenumber%,] [expressionlist] [{; | ,}]
  • LPRINT [expressionlist] [{; | ,}]
Description/Parameter(s)
filenumber% The number of an open file. If you don't specify a file number, PRINT writes to the screen.
expressionlist A list of one or more numeric or string expressions to print.
{; | ,} Determines where the next output begins:
  • ; means print immediately after the last value.
  • , means print at the start of the next print zone.
  •   Print zones are 14 characters wide.
Example
OPEN "TEST.DAT" FOR OUTPUT AS #1 PRINT #1, USING "##.### "; 12.12345 CLOSE OPEN "TEST.DAT" FOR INPUT AS #1 INPUT #1, a$ PRINT a$ LPRINT "This is a line"; 1 LPRINT "This is a line", LPRINT 2
Syntax
  • PRINT # filenumber, [USING stringexpression;] expressionlist [{,|;}]
Description/Parameter(s)

The filenumber is the number specified when the file was opened for output. The stringexpression consists of formatting characters as described under PRINT USING . The expressions in expressionlist are the numeric or string expressions to be written to the file. Spaces, commas, and semicolons in the expressionlist have the same meaning they have in a PRINT statement.

If you omit expressionlist, the PRINT # statement prints a blank line in the file.

PRINT # works like PRINT and writes an image of the data to the file, just as the data would be displayed on the terminal screen. For this reason, be careful to delimit the data so it is output correctly. If you use commas as delimiters, the blanks between print fields are also written to the file.

Example

This example shows the effects of using data delimiters with PRINT #.

CLS ' Clear screen A$ = "CAMERA, AUTOFOCUS" : B$= "September 20, 1985": C$ = "42" Q$ = CHR$(34) OPEN "INVENT.DAT" FOR OUTPUT AS #1 'Open INVENT.DAT for writing 'Write A$, B$, C$ without delimiters. PRINT #1, A$; B$; C$ 'Write A$, B$, C$ with delimiters. PRINT #1, Q$; A$; Q$; Q$; B$; Q$; Q$; C$; Q$ CLOSE #1 OPEN "INVENT.DAT" FOR INPUT AS #1 'Open INVENT.DAT for reading. FOR I% = 1 TO 2 'Read first two records and print. INPUT #1, First$, Second$, Third$ PRINT First$; TAB(30); Second$; TAB(60); Third$ : PRINT NEXT CLOSE #1

Sample Output:

CAMERA AUTOFOCUSSeptember 20 198542 CAMERA, AUTOFOCUS September 20, 1985 42
Syntax
  • PRINT #filenumber%, [USING formatstring$;] expressionlist[{,|;}]
Description/Parameter(s)
filenumber% The number used to open the sequential file.
expressionlist Numeric or string expressions to be written to the file.
formatstring$ Specifies the exact format in which values are written to the file. See Details for the meaning of characters in formatstring.
  • Characters in formatstring$ have the following meaning:
    Characters Used to Format a Numeric Expression
    #Digit position.
    .Decimal point position.
    ,Placed left of the decimal point, prints a comma every third digit.
    +Position of number sign.
    ^^^^Prints in exponential format.
    -Placed after digit, prints trailing sign for negative numbers.
    $$Prints leading $.
    **Fills leading spaces with *.
    **$Combines ** and $$.
    Characters Used to Format a String Expression
    &Prints entire string.
    !Prints only the first character of the string.
    \ \Prints first n characters, where n is the number of blanks between slashes + 2.
    Characters Used to Print Literal Characters from formatstring
    _Prints the following formatting character as a literal.
     Any character not in this table is printed as a literal.

Usage Notes

  • Spaces, commas, and semicolons in the expressionlist have the same meaning they have in a PRINT statement.
  • If you omit expressionlist, the PRINT # statement prints a blank line in the file.
  • PRINT # works like PRINT and writes an image of the data to the file, just as the data would be displayed on the terminal screen. For this reason, be careful to delimit the data so it is output correctly. If you use commas as delimiters, the blanks between print fields are also written to the file.
Example

This example shows the effect of using data delimiters with the PRINT # statement.

CONST A$ = "CAMERA, AUTOFOCUS", B$ = "September 20, 1989", C$ = "42" Q$ = CHR$(34) CLS 'Clear screen. OPEN "INVENT.DAT" FOR OUTPUT AS #1 'Open INVENT.DAT for writing. 'Write A$, B$, C$ without delimiters. PRINT #1, A$; B$; C$ 'Write A$, B$, C$ with delimiters. PRINT #1, Q$; A$; Q$; Q$; B$; Q$; Q$; C$; Q$ CLOSE #1 OPEN "INVENT.DAT" FOR INPUT AS #1 'Open INVENT.DAT for reading. FOR I% = 1 TO 2 'Read first two records and print. INPUT #1, First$, Second$, Third$ PRINT First$; TAB(30); Second$; TAB(60); Third$: PRINT NEXT CLOSE #1 'Remove file from disk. KILL "INVENT.DAT"

Sample Output:

CAMERA AUTOFOCUSSeptember 20 198942 CAMERA, AUTOFOCUS September 20, 1989 42