Writes formatted text to the file associated with file number #n. The expression is formatted identically to console PRINT and then written using the Win32 WriteFile function with the stored file handle. Supports the same expression types as console PRINT: strings, integers, floating-point values, and multiple comma/semicolon-separated expressions.
Unlike console PRINT, the file handle comes from the compiler's file handle table rather than GetStdHandle(STD_OUTPUT_HANDLE). All formatting (integer via wsprintfA, float via _gcvt, string direct write) is identical.
Parameters
Parameter
Description
n
The integer file number to write to. The file must have been opened for OUTPUT, APPEND, BINARY, or RANDOM access.
expr
Any printable expression: STRING literal or variable, numeric literal or variable (LONG, SINGLE, DOUBLE, etc.), or a comma/semicolon-separated list of expressions.
; (semicolon separator)
Separates multiple expressions. A trailing semicolon suppresses the automatic CRLF at the end of the PRINT statement.
(no trailing semicolon)
Appends a carriage return + line feed (CRLF) after the last expression.
PRINT #n, (bare)
A bare PRINT #n, with no expression outputs a blank line (just CRLF) to the file.
Example
FUNCTION PBMAIN() AS LONG
LOCAL x AS LONG
LOCAL name AS STRING
x = 42
name = "PowerBASIC"
OPEN "output.txt" FOR OUTPUT AS #1
' Write a string literal
PRINT #1, "Hello World"
' Write a variable with text
PRINT #1, "The value of x is "; x
' Write multiple items on one line
PRINT #1, "Name: "; name; " Value: "; x
' Write a blank line
PRINT #1,
' Write without trailing newline
PRINT #1, "Loading";
PRINT #1, "..."
PRINT #1, "Done!"
CLOSE #1
FUNCTION = 0
END FUNCTION
Output File Contents (output.txt)
Hello World
The value of x is 42
Name: PowerBASIC Value: 42
Loading...Done!
; String address resolved via pb_cg_emit_string_address_len
; into RDX (buffer) and R8 (length)
; Then same WriteFile path as above with file handle in RCX