ZetCode

FreeBasic LOF Keyword

Last modified October 18, 2026

The FreeBasic LOF function returns the length of an open file in bytes. It stands for "Length Of File" and is essential for file operations.

Basic Definition

LOF is a built-in FreeBasic function that takes a file number as its parameter. It returns a Longint (64-bit integer) representing the file's size.

The file must be opened before using LOF. It works with binary and text files. The returned value indicates the exact byte count of the file.

Getting File Size

This basic example shows how to get the size of a file using LOF.

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

Dim fileSize As Long = LOF(fileNum)
Print "File size: "; fileSize; " bytes"

Close #fileNum

We first get a free file handle with FreeFile. Then we open the file in binary mode. LOF returns the file size which we print. Finally, we close the file.

Checking Empty Files

LOF can determine if a file is empty by checking if its length is zero.

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

If LOF(f) = 0 Then
    Print "The file is empty"
Else
    Print "File contains data"
End If

Close #f

This code opens a file and checks its length. If LOF returns 0, we know the file is empty. This is useful for validation before processing files.

Reading Entire File

LOF helps read complete files by determining the needed buffer size.

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

Dim size As Long = LOF(f)
Dim buffer As String = Space(size)
Get #f, , buffer

Print buffer
Close #f

We get the file size with LOF to create a buffer of the exact size. Then we read the entire content with Get. This method is efficient for small to medium files.

File Comparison

LOF can quickly compare files by size before deeper comparison.

lof_compare.bas
Dim f1 As Integer = FreeFile()
Dim f2 As Integer = FreeFile()

Open "file1.txt" For Binary As #f1
Open "file2.txt" For Binary As #f2

If LOF(f1) <> LOF(f2) Then
    Print "Files are different sizes"
Else
    Print "Files are same size (content may differ)"
End If

Close #f1, #f2

This example compares two files' sizes. If sizes differ, the files are certainly different. Equal sizes do not guarantee identical content but help optimize comparisons.

Progress Indicator

LOF can help create file operation progress indicators.

lof_progress.bas
Dim source As Integer = FreeFile()
Open "largefile.bin" For Binary As #source

Dim total As Long = LOF(source)
Dim copied As Long = 0
Dim buffer(1023) As Byte

Do While copied < total
    Dim toRead As Long = IIf(total - copied > 1024, 1024, total - copied)
    Get #source, , buffer()
    copied += toRead
    Print "Progress: "; (copied * 100) \ total; "%"
Loop

Close #source

This code shows a progress percentage while reading a large file. LOF gives the total size to calculate progress. The loop updates until the entire file is processed.

File Truncation

LOF can be used to compute the new size when shortening a file.

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

Dim newSize As Longint = LOF(f) \ 2  ' Half of the current size
Dim buffer As String = Space(newSize)
Get #f, 1, buffer
Close #f

Dim g As Integer = FreeFile()
Open "logfile.txt" For Output As #g
Print #g, buffer;
Close #g

Open "logfile.txt" For Binary As #g
Print "New size: "; LOF(g)
Close #g

Here we use LOF to get the current size and read the first half of the file into a buffer. Reopening the file for output discards its old contents, and writing the buffer back leaves only the first half. Putting an empty string with PUT does not shorten a file, so it is not used here.

Memory Mapping

LOF helps when working with memory-mapped files by providing size info. The example uses the Windows API, so it only builds and runs on Windows.

lof_memorymap.bas
#Include Once "windows.bi"
#Include Once "file.bi"

Dim f As Integer = FreeFile()
Open "data.bin" For Output As #f
Print #f, "mapping data"
Close #f

Open "data.bin" For Binary As #f
Dim size As Long = LOF(f)
Dim hMap As HANDLE = CreateFileMapping(Cast(HANDLE, FileAttr(f, fbFileAttrHandle)), _
    NULL, PAGE_READONLY, 0, size, NULL)

If hMap Then
    Print "Memory mapping created for "; size; " bytes"
    CloseHandle(hMap)
End If

Close #f

FileAttr(f, fbFileAttrHandle) returns the underlying OS handle for the open file, which CreateFileMapping needs. The example first writes a small file so that the mapping has a nonzero size. The handle and the mapping are closed before the program ends.

Best Practices

This tutorial covered the FreeBasic LOF function with practical examples showing its usage in different file operations.

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.