SEEK #n, positionpb_cg_emit_seek (pb_codegen.c:6132)The SEEK statement sets the file pointer for file number #n to the specified 1-based record position. This controls where the next GET or PUT operation will read from or write to. The meaning of position depends on the file mode:
(position - 1) * LEN, where LEN is the record length specified in the OPEN statement.(position - 1) * 1 (or equivalently, position - 1).Internally, the 1-based position is converted to a 0-based byte offset and the Win32 SetFilePointer function is called with FILE_BEGIN to reposition the file pointer.
Note: Do not confuse the SEEK statement with the SEEK function. The function form SEEK(#n) returns the current byte position without moving the file pointer. See LOC / SEEK Functions for details.
| Parameter | Description |
|---|---|
n | The integer file number. The file must be open for BINARY or RANDOM access. |
position | A numeric expression giving the 1-based record position (RANDOM) or byte position (BINARY). If position is less than 1, it is clamped to 1. |
TYPE Record
id AS LONG
nam AS STRING * 20
END TYPE
FUNCTION PBMAIN() AS LONG
LOCAL rec AS Record
LOCAL i AS LONG
OPEN "data.dat" FOR RANDOM AS #1 LEN = 24
' Read records in reverse order
FOR i = 10 TO 1 STEP -1
SEEK #1, i ' jump to record i
GET #1, rec ' read record at that position
PRINT rec.id, rec.nam
NEXT i
CLOSE #1
FUNCTION = 0
END FUNCTION
OPEN "data.bin" FOR BINARY AS #2 SEEK #2, 100 ' seek to byte position 100 (offset 99) GET #2, var ' read next value at byte 100 SEEK #2, 200 ' seek to byte position 200 (offset 199) PUT #2, var2 ' write at byte 200 CLOSE #2
; Convert 1-based position to 0-based byte offset ; RANDOM mode: offset = (position - 1) * record_length ; BINARY mode: offset = (position - 1) * 1 cmp rax, 1 ; check position >= 1 jge __pb_seek_positive xor rax, rax ; clamp to 0 if < 1 jmp __pb_seek_ready __pb_seek_positive: sub rax, 1 ; 1-based -> 0-based imul rax, <multiplier> ; record_length (RANDOM) or 1 (BINARY) __pb_seek_ready: mov rcx, [rbp - handle_offset] ; file handle mov rdx, rax ; lDistanceToMove (low 32 bits) xor r8, r8 ; lpDistanceToMoveHigh = NULL (no 64-bit seek) xor r9, r9 ; dwMoveMethod = FILE_BEGIN (0) call [__imp_SetFilePointer]
| SEEK Position | Mode | Multiplier | SetFilePointer offset |
|---|---|---|---|
| 1 | BINARY | 1 | 0 (beginning of file) |
| 100 | BINARY | 1 | 99 |
| 1 | RANDOM LEN=40 | 40 | 0 (beginning of file) |
| 5 | RANDOM LEN=40 | 40 | 160 |
| 1 | RANDOM LEN=128 | 128 | 0 (beginning of file) |
| 3 | RANDOM LEN=128 | 128 | 256 |
| Function | DLL | Purpose |
|---|---|---|
SetFilePointer | kernel32.dll | Move the file pointer to a specified byte offset from the beginning of the file |