ZetCode

FreeBasic Seek Keyword

last modified June 16, 2025

The FreeBasic Seek keyword is used to move the file position pointer within an open file. It allows random access to file contents by positioning the pointer at a specific byte location.

Basic Definition

In FreeBasic, Seek is a statement that changes the current position in a file. It works with both binary and text files opened for random access.

The Seek function returns the current position of the file pointer. Together, these allow precise navigation through file contents. File positions are 1-based in FreeBasic (first byte is position 1).

Seek Statement Syntax

The Seek statement has this syntax:

Seek #fileNumber, position

fileNumber is the number used when opening the file. position is the new position (1-based) for the file pointer.

Seek Function Syntax

The Seek function has this syntax:

currentPosition = Seek(fileNumber)

It returns the current position of the file pointer for the specified file. This is useful for tracking your location in a file.

Basic Seek Example

This example demonstrates basic file positioning with Seek.

seek_basic.bas
Dim f As Integer = FreeFile()
Open "test.txt" For Output As #f
Print #f, "Hello World"
Close #f

Open "test.txt" For Input As #f
Seek #f, 7
Dim s As String
Input #f, s
Close #f

Print "Read from position 7: "; s

We first create a file with some text. Then we reopen it and use Seek to position at byte 7 before reading. This reads "World" from the file. The position is 1-based, so byte 7 is the 'W' in "World".

Seek with Binary Files

Seek is particularly useful when working with binary files.

seek_binary.bas
Dim f As Integer = FreeFile()
Open "data.bin" For Binary As #f

' Write three integers at specific positions
Seek #f, 1
Put #f, , 100
Seek #f, 5
Put #f, , 200
Seek #f, 9
Put #f, , 300

' Read the second integer
Seek #f, 5
Dim value As Integer
Get #f, , value
Close #f

Print "Value at position 5: "; value

This creates a binary file and writes integers at specific positions. We then seek to position 5 and read the value stored there. Each integer occupies 4 bytes in FreeBasic (by default), so we space them accordingly.

Using Seek Function

This example shows how to use the Seek function to track file position.

seek_function.bas
Dim f As Integer = FreeFile()
Open "positions.txt" For Output As #f

Print #f, "First line"
Print "Current position: "; Seek(f)
Print #f, "Second line"
Print "Current position: "; Seek(f)
Print #f, "Third line"
Print "Current position: "; Seek(f)

Close #f

As we write lines to the file, we use the Seek function to monitor the current position. Each Print statement advances the position by the number of characters written plus line endings.

Random Access with Seek

Seek enables true random access to file contents.

seek_random.bas
Dim f As Integer = FreeFile()
Open "random.txt" For Output As #f
For i As Integer = 1 To 10
    Print #f, "Record "; i
Next
Close #f

' Read records in reverse order
Open "random.txt" For Input As #f
Dim fileSize As Long = Lof(f)
For i As Integer = 10 To 1 Step -1
    Seek #f, (i-1)*9 + 1  ' Each record is 9 bytes ("Record X" + newline)
    Dim s As String
    Line Input #f, s
    Print s
Next
Close #f

We first create a file with 10 records. Then we reopen it and read the records in reverse order by calculating their positions. Each record is 9 bytes long (including newline), so we multiply the index by 9.

Seek with Large Files

Seek can handle large files by using Long or Integer64 positions.

seek_large.bas
Dim f As Integer = FreeFile()
Open "large.bin" For Binary As #f

' Write data at large positions
Seek #f, 1000000
Put #f, , 12345
Seek #f, 2000000
Put #f, , 67890

' Read back
Seek #f, 1000000
Dim value1 As Integer
Get #f, , value1

Seek #f, 2000000
Dim value2 As Integer
Get #f, , value2

Close #f

Print "Values: "; value1; " and "; value2

This demonstrates Seek working with large file positions (millions of bytes). FreeBasic handles these positions correctly, allowing access to any part of large files.

Seek and File Size

Seek can be used with Lof to navigate relative to file size.

seek_filesize.bas
Dim f As Integer = FreeFile()
Open "example.txt" For Binary As #f

' Write some data
For i As Integer = 1 To 100
    Put #f, , CByte(i)
Next

' Read last 10 bytes
Seek #f, Lof(f) - 9
Dim lastBytes(1 To 10) As Byte
Get #f, , lastBytes()

Close #f

Print "Last 10 bytes:"
For i As Integer = 1 To 10
    Print lastBytes(i); " ";
Next

We create a binary file with 100 bytes, then use Seek with Lof (length of file) to position near the end. This pattern is useful for reading file footers or trailers in many file formats.

Error Handling with Seek

Proper error handling is important when using Seek.

seek_errors.bas
Dim f As Integer = FreeFile()
Open "test.bin" For Binary As #f

' Try to seek beyond file end
On Error Goto ErrorHandler
Seek #f, 100000

Print "Seek successful"
Close #f
Exit Sub

ErrorHandler:
Print "Error "; Err; " occurred: "; Error(Err)
Close #f
End

This shows how to handle Seek errors, particularly when attempting to position beyond the end of a file. The On Error statement catches any runtime errors that occur during Seek operations.

Best Practices

This tutorial covered the FreeBasic Seek keyword with practical examples showing file positioning in different scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all FreeBasic Tutorials.