INPUT #n Statement

Syntax

Syntax: INPUT #n, variable
Source: pb_cg_emit_read_file_line (pb_codegen.c:5291)

Description

Reads a line of text from the file associated with file number #n into a variable. The file must have been opened for INPUT or BINARY access. Internally, this uses the Win32 ReadFile function to read bytes from the file handle into a stack buffer. After reading, the buffer is NUL-terminated and assigned to the target variable.

When the target variable is numeric (LONG, SINGLE, DOUBLE, etc.), the input string is automatically converted to the numeric type using the equivalent of VAL(). When the target is a STRING, the raw text is assigned directly.

Parameters

ParameterDescription
nThe integer file number to read from. The file must be opened for INPUT, BINARY, or RANDOM.
variableThe variable to receive input. Can be a STRING (receives one line of text) or a numeric type (text is converted via VAL).

Example

FUNCTION PBMAIN() AS LONG
    LOCAL name AS STRING
    LOCAL age AS LONG
    LOCAL score AS SINGLE

    OPEN "people.txt" FOR INPUT AS #1

    INPUT #1, name        ' read a name string
    INPUT #1, age         ' read a number
    INPUT #1, score       ' read a floating-point number

    PRINT "Name: "; name
    PRINT "Age: "; age
    PRINT "Score: "; score

    CLOSE #1
    FUNCTION = 0
END FUNCTION

Input File Contents (people.txt)

Alice Johnson
42
98.5

Program Output

Name: Alice Johnson
Age: 42
Score: 98.5

Generated Assembly

; 1. Read from file handle via ReadFile
mov    rcx, [rbp - handle_offset]        ; file handle from handle table
lea    rdx, [rbp - buffer_offset]         ; stack buffer for input
mov    r8, <max_count>                   ; maximum bytes to read (256-1024)
lea    r9, [rbp - file_read_count]        ; lpNumberOfBytesRead
mov    [rsp + 32], 0                      ; lpOverlapped = NULL
call   [__imp_ReadFile]

; 2. NUL-terminate the read buffer
mov    r10, [rbp - file_read_count]       ; actual bytes read
lea    r11, [rbp - buffer_offset]
add    r11, r10
mov    byte [r11], 0                      ; buffer[bytesRead] = 0

; 3. Assign to target variable
; For STRING target: copy NUL-terminated buffer to string descriptor
; For numeric target: call VAL conversion then store in numeric variable

Numeric Target Conversion

; After reading and NUL-terminating as above:
; The string buffer is passed through the runtime VAL() equivalent
; which parses the text and returns the numeric value.
;   Integer:  atoi() or strtol() equivalent
;   Float:    atof() or strtod() equivalent
; The result is stored directly in the numeric variable's stack slot.

Imported WinAPI Functions

FunctionDLLPurpose
ReadFilekernel32.dllRead bytes from file handle into a buffer

Notes