GET Statement
Syntax
Syntax:
Source:
GET #n, [position,] variableSource:
pb_cg_emit_get (pb_codegen.c:780)Description
Reads binary data from the file associated with file number #n into a variable. The file must be opened for BINARY or RANDOM access. If the optional position is specified, the file pointer is first set to that position before reading. In RANDOM mode, the position represents a 1-based record number (byte offset = (position - 1) * LEN). In BINARY mode, the position represents a 1-based byte position.
The number of bytes read is determined by the data type of the target variable: 1 byte for BYTE, 2 bytes for WORD/INTEGER, 4 bytes for LONG, 8 bytes for QUAD/DOUBLE, and the record length for UDTs. V19 uses the packed scalar byte size instead of a fixed 8-byte default.
Parameters
| Parameter | Description |
|---|---|
n | The integer file number. Must be opened for BINARY or RANDOM access. |
position | (Optional) 1-based record position in RANDOM mode, or 1-based byte position in BINARY mode. If omitted, the current file pointer is used. |
variable | The variable to read data into. Can be any numeric type (BYTE, WORD, LONG, QUAD, SINGLE, DOUBLE), a fixed-length STRING, or a UDT with only fixed-size fields. |
Example
TYPE Employee
id AS LONG
nam AS STRING * 32
salary AS SINGLE
END TYPE
FUNCTION PBMAIN() AS LONG
LOCAL emp AS Employee
LOCAL val AS LONG
' Binary mode: read LONG at byte position 100
OPEN "data.bin" FOR BINARY AS #1
GET #1, 100, val ' read 4 bytes at offset 99 (0-based)
CLOSE #1
' Random mode: read record 5 (each 40 bytes)
OPEN "employees.dat" FOR RANDOM AS #2 LEN = 40
GET #2, 5, emp ' read record 5 (offset 4 * 40 = 160)
PRINT emp.nam
CLOSE #2
FUNCTION = 0
END FUNCTION
Generated Assembly
GET with Position (Seek + Read)
; Step 1: If position specified, seek to correct offset ; RANDOM mode: offset = (position - 1) * record_length ; BINARY mode: offset = (position - 1) * sizeof(variable) cmp rax, 1 ; check position >= 1 jge __pb_get_compute_offset xor rax, rax ; clamp to 0 if < 1 jmp __pb_get_seek __pb_get_compute_offset: sub rax, 1 ; 1-based -> 0-based imul rax, <record_length_or_size> ; multiply by record/element size __pb_get_seek: mov rcx, [rbp - handle_offset] mov rdx, rax ; distance to move xor r8, r8 ; 0 for low DWORD mov r9, 0 ; FILE_BEGIN call [__imp_SetFilePointer] ; Step 2: Read data into target variable mov rcx, [rbp - handle_offset] ; file handle mov rdx, target_ptr ; address of target variable mov r8, <element_size> ; bytes to read (depends on type) lea r9, [rbp - file_read_count] ; &bytesRead mov [rsp + 32], 0 ; lpOverlapped = NULL call [__imp_ReadFile]
GET without Position (Sequential Read)
; No seek: read from current file pointer mov rcx, [rbp - handle_offset] ; file handle mov rdx, target_ptr ; address of target variable mov r8, <element_size> ; bytes to read lea r9, [rbp - file_read_count] ; &bytesRead mov [rsp + 32], 0 ; lpOverlapped = NULL call [__imp_ReadFile]
Scalar Size Reference V19
| Data Type | Bytes Read | Example |
|---|---|---|
| BYTE | 1 | GET #1, , b |
| INTEGER / WORD | 2 | GET #1, , w |
| LONG / SINGLE | 4 | GET #1, , l |
| QUAD / DOUBLE | 8 | GET #1, , q |
| STRING * n | n | GET #1, , s |
| UDT | LEN(udt) | GET #1, , rec |
Imported WinAPI Functions
| Function | DLL | Purpose |
|---|---|---|
SetFilePointer | kernel32.dll | Set file pointer before reading (when position is specified) |
ReadFile | kernel32.dll | Read binary data from file into target variable |
Notes
- UDTs with dynamic STRING or WSTRING fields are rejected in BINARY/RANDOM mode V19.
- Use
SEEK #n, posto position the file pointer separately from a GET. LOC(#n)returns the current position (1-based) in BINARY mode, or current record number in RANDOM mode.