LINE INPUT #n Statement
Syntax
Syntax:
Source:
LINE INPUT #n, stringvar$Source:
pb_cg_emit_read_file_line (pb_codegen.c:5291), reworked in V13Description
Reads an entire line of text from the file associated with file number #n into a STRING variable, discarding the trailing line-ending characters (CR, LF, or CRLF). Unlike INPUT #n, LINE INPUT does not interpret commas or quotes as delimiters—the entire line is read verbatim.
The V13 implementation uses a character-by-character read loop that correctly handles all common line-ending conventions: CR+LF (Windows), LF-only (Unix), and CR-only (classic Mac). V19 adds encoding-aware reading: when the file is opened with ENCODING UTF16LE or ENCODING UTF16LE_BOM, LINE INPUT reads UTF-16 code units and converts them to UTF-8-compatible ANSI for STRING targets.
Parameters
| Parameter | Description |
|---|---|
n | The integer file number to read from. The file must be opened for INPUT, BINARY, or RANDOM access. |
stringvar$ | A STRING variable to receive the line text. The trailing line-ending characters are stripped. |
Example
FUNCTION PBMAIN() AS LONG
LOCAL ln AS STRING
LOCAL lineNum AS LONG
lineNum = 0
OPEN "data.txt" FOR INPUT AS #1
WHILE NOT EOF(1)
INCR lineNum
LINE INPUT #1, ln
PRINT "Line "; lineNum; ": "; ln
WEND
CLOSE #1
FUNCTION = 0
END FUNCTION
Encoding-Aware Example V19
' Read a UTF-16LE encoded file with BOM OPEN "unicode.txt" FOR INPUT ENCODING UTF16LE_BOM AS #1 LINE INPUT #1, s$ ' reads UTF-16LE, converts to ANSI/UTF-8 STRING PRINT s$ CLOSE #1
Line Ending Behavior
| Bytes in File | Result in var$ | Origin |
|---|---|---|
Hello\r\n (0D 0A) | "Hello" | Windows / DOS |
World\n (0A) | "World" | Unix / Linux |
Line\r (0D) | "Line" | Classic Mac OS |
NoNewline (no terminator) | "NoNewline" | Final line before EOF |
\r\n\r\n (blank line) | "" (empty string) | Blank line in text file |
Generated Assembly
Character-by-Character Read Loop (V13)
; Read one byte at a time until CR, LF, or EOF __pb_liread_loop: mov rcx, [rbp - handle_offset] ; file handle lea rdx, [rbp - one_byte_temp] ; single-byte buffer mov r8, 1 ; read exactly 1 byte lea r9, [rbp - bytes_read] ; &bytesRead mov [rsp + 32], 0 ; lpOverlapped = NULL call [__imp_ReadFile] cmp rax, 0 ; ReadFile returns 0 on failure je __pb_liread_eof ; treat as EOF mov al, byte [rbp - one_byte_temp] ; load the byte cmp al, 13 ; is it CR? je __pb_liread_check_lf ; maybe CRLF ahead cmp al, 10 ; is it LF? je __pb_liread_done ; line complete (LF-only) ; Not a line ending: store the byte lea rdi, [rbp - linebuf_offset] ; output buffer add rdi, [rbp - linebuf_pos] ; current write position mov [rdi], al ; store byte inc qword [rbp - linebuf_pos] ; advance position jmp __pb_liread_loop ; read next byte __pb_liread_check_lf: ; CR found. Peek ahead for LF (CRLF handling). mov rcx, [rbp - handle_offset] lea rdx, [rbp - one_byte_temp] mov r8, 1 lea r9, [rbp - bytes_read] mov [rsp + 32], 0 call [__imp_ReadFile] cmp rax, 0 je __pb_liread_done ; CR at EOF mov al, byte [rbp - one_byte_temp] cmp al, 10 ; LF follows CR? je __pb_liread_done ; consume the LF ; Not LF: push it back conceptually (simple approach: ignore) __pb_liread_done: ; NUL-terminate the line buffer mov r10, [rbp - linebuf_pos] lea r11, [rbp - linebuf_offset] add r11, r10 mov byte [r11], 0 ; buffer[pos] = 0 ; Assign the NUL-terminated buffer to target STRING variable __pb_liread_eof: ; EOF reached before any line ending ; Just NUL-terminate whatever was accumulated and return
UTF-16LE Encoding-Aware Read V19
; When ENCODING UTF16LE or UTF16LE_BOM is set: ; 1. Read 2 bytes at a time (one UTF-16 code unit) ; 2. Convert UTF-16LE code unit to multi-byte UTF-8 sequence ; 3. Check for CR (0x000D) and LF (0x000A) as 16-bit values ; 4. When line ending found, convert accumulated UTF-16LE to ANSI STRING ; using WideCharToMultiByte(CP_ACP, 0, wbuf, wlen, buf, buflen, 0, 0)
Comparison: INPUT# vs LINE INPUT#
| Aspect | INPUT #n | LINE INPUT #n |
|---|---|---|
| Target variable | STRING or numeric | STRING only |
| Comma handling | Field separator | Literal character |
| Quote handling | String delimiter | Literal character |
| Line endings | Any whitespace | CR / LF / CRLF |
| Encoding-aware | No | V19 Yes |
Imported WinAPI Functions
| Function | DLL | Purpose |
|---|---|---|
ReadFile | kernel32.dll | Read single bytes in loop for line detection |
WideCharToMultiByte | kernel32.dll | V19 Convert UTF-16LE to ANSI for encoding-aware reads |