ZetCode

FreeBasic Loc Keyword

last modified June 16, 2025

The FreeBasic Loc keyword returns the current position within an open file. It is essential for file handling operations where you need to track or manipulate the file pointer position.

Basic Definition

In FreeBasic, Loc is a function that returns a Long integer representing the current position in a file. The position is measured in bytes from the beginning of the file (position 0).

Loc works with files opened in binary or random access modes. It's commonly used with Seek to navigate through files and with LOF to determine file sizes.

Getting Current File Position

This basic example shows how to use Loc to get the current position in a file.

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

Print "Initial position: "; Loc(f)

Dim s As String = "Hello"
Put #f, , s

Print "After writing: "; Loc(f)
Close #f

We open a file in binary mode and check the initial position (0). After writing a string, Loc returns the new position at the end of the written data. The position advances automatically during I/O operations.

Comparing Loc with LOF

This example demonstrates the relationship between Loc and LOF (Length Of File).

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

Print "File size: "; LOF(f)
Print "Current position: "; Loc(f)

Seek #f, LOF(f) \ 2
Print "Middle position: "; Loc(f)

Close #f

We open a binary file and compare its total size (LOF) with the current position (Loc). Then we seek to the middle of the file and verify the new position. This shows how to navigate files using position information.

Random Access File Handling

Loc is particularly useful when working with random access files.

loc_random.bas
Type Person
    name As String * 20
    age As Integer
End Type

Dim f As Integer = FreeFile()
Open "people.dat" For Random As #f Len = SizeOf(Person)

Dim p As Person
p.name = "John Doe"
p.age = 30

Put #f, 3, p  ' Write to record 3
Print "After write: "; Loc(f)

Get #f, 1, p  ' Read record 1
Print "After read: "; Loc(f)

Close #f

In random access mode, Loc returns the current record number rather than a byte position. We write to record 3 and read from record 1, showing how Loc tracks our position in the file.

File Position Tracking

This example shows how to use Loc to track progress through a file.

loc_tracking.bas
Dim f As Integer = FreeFile()
Open "largefile.dat" For Binary As #f

Dim fileSize As Long = LOF(f)
Dim chunk(1023) As Byte

Do While Loc(f) < fileSize
    Get #f, , chunk
    Print "Progress: "; (Loc(f) * 100) \ fileSize; "%"
Loop

Close #f

We read a large file in chunks and use Loc to calculate and display progress. The percentage shows how much of the file we've processed. This technique is useful for progress bars in file operations.

Seeking with Loc

This example combines Loc with Seek to navigate through a file.

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

' Store initial position
Dim startPos As Long = Loc(f)

' Read first 10 bytes
Dim header As String * 10
Get #f, , header

' Return to start
Seek #f, startPos

' Verify position
Print "Position after seek: "; Loc(f)

Close #f

We store the initial position, read some data, then return to the start using Seek. Loc verifies our position before and after seeking. This pattern is useful when you need to re-read data.

Binary File Editing

Loc helps when making targeted modifications to binary files.

loc_edit.bas
Dim f As Integer = FreeFile()
Open "config.cfg" For Binary As #f

' Find signature position
Dim sig As String * 4 = "CFG1"
Dim foundPos As Long = -1
Dim buf As String * 4

Do While Loc(f) < LOF(f) - 3
    Get #f, , buf
    If buf = sig Then
        foundPos = Loc(f) - 4
        Exit Do
    End If
Loop

If foundPos >= 0 Then
    Print "Signature found at: "; foundPos
    Seek #f, foundPos + 8
    Put #f, , 42  ' Update value
Else
    Print "Signature not found"
End If

Close #f

We search for a signature pattern in a binary file using Loc to track our position. When found, we navigate to an offset from the signature to make an update. This demonstrates precise binary file editing.

Error Handling with Loc

This example shows how to use Loc in error handling scenarios.

loc_error.bas
Function ReadFixedString(f As Integer, length As Integer) As String
    Dim posBefore As Long = Loc(f)
    Dim result As String = Space(length)
    
    Get #f, , result
    
    If Loc(f) - posBefore <> length Then
        Seek #f, posBefore
        Return ""  ' Indicate failure
    End If
    
    Return result
End Function

Dim f As Integer = FreeFile()
Open "test.dat" For Binary As #f

Dim data As String = ReadFixedString(f, 10)
If data = "" Then
    Print "Error reading string at position: "; Loc(f)
Else
    Print "Read: "; data
End If

Close #f

The function attempts to read a fixed-length string and uses Loc to verify the read was successful. If not, it restores the original position. This ensures the file pointer remains consistent after errors.

Best Practices

This tutorial covered the FreeBasic Loc keyword with practical examples showing its usage in different file handling 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.