ZetCode

FreeBasic LOF Keyword

last modified June 16, 2025

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 handle as its parameter. It returns a Long 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 don't 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 combined with PUT can truncate files at specific positions.

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

Dim newSize As Long = LOF(f) \ 2  ' Cut to half size
Put #f, newSize + 1, ""

Close #f

Here we use LOF to get the current size, then truncate the file to half its size. The PUT statement at the new end position effectively cuts off the rest of the file.

Memory Mapping

LOF helps when working with memory-mapped files by providing size info.

lof_memorymap.bas
#Include Once "windows.bi"

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

Dim size As Long = LOF(f)
Dim hMap As HANDLE = CreateFileMapping(GetOSfHandle(f), NULL, PAGE_READONLY, 0, size, NULL)

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

Close #f

This advanced example shows how LOF provides the crucial size parameter for memory mapping. The Windows API needs the exact file size to create the mapping object.

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.