PRINT #n Statement

Syntax

Syntax: PRINT #n, expr [; expr ...]
Source: pb_cg_emit_writefile_handle (pb_codegen.c:5182)

Description

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

ParameterDescription
nThe integer file number to write to. The file must have been opened for OUTPUT, APPEND, BINARY, or RANDOM access.
exprAny 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!

Generated Assembly

Integer Output to File

; 1. Format integer via wsprintfA (same as console PRINT)
lea    rcx, [rbp - intbuf_offset]   ; destination buffer
lea    rdx, [__pb_fmt_i64]          ; format string "%I64d"
mov    r8, rax                      ; integer value
call   [__imp_wsprintfA]            ; wsprintfA(buf, fmt, val)
mov    [rbp - print_len_offset], rax ; save formatted length

; 2. Write to file via WriteFile (uses file handle, NOT GetStdHandle)
mov    rcx, [rbp - handle_offset]   ; file handle from handle table
mov    rdx, [rbp - print_ptr_offset] ; lpBuffer (formatted text)
mov    r8, [rbp - print_len_offset]  ; nBytesToWrite
lea    r9, [rbp - written_offset]    ; lpBytesWritten
mov    [rsp + 32], 0                 ; lpOverlapped = NULL
call   [__imp_WriteFile]

String Output to File

; 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

CRLF (Newline) to File

lea    rdx, [__pb_crlf]            ; "\r\n" in .rdata section
mov    r8, 2                        ; 2 bytes
mov    rcx, [rbp - handle_offset]   ; file handle
; then WriteFile

Float Output to File

; Format float to string first
lea    r8, [rbp - intbuf_offset]    ; buffer
mov    rdx, 15                      ; precision
call   [__imp__gcvt]                ; _gcvt(value, 15, buf)
mov    [rbp - print_ptr_offset], rax
mov    rcx, rax
call   [__imp_lstrlenA]
mov    [rbp - print_len_offset], rax
; then WriteFile with file handle

Comparison: Console vs File PRINT

AspectPRINT (Console)PRINT #n (File)
Handle sourceGetStdHandle(-11)[rbp - handle_offset]
Expression formattingIdentical (wsprintfA / _gcvt)Identical (wsprintfA / _gcvt)
Write functionWriteFileWriteFile
Semicolon suppressionYesYes

Imported WinAPI Functions

FunctionDLLPurpose
WriteFilekernel32.dllWrite bytes to file using stored file handle
wsprintfAuser32.dllFormat integer to string buffer
lstrlenAkernel32.dllGet ANSI string length
_gcvtmsvcrt.dllConvert float to string