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.
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.
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.
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.
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.
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.
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.
#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
- Error Handling: Always check if files opened successfully before using LOF.
- Large Files: LOF returns Long values, supporting files up to 2GB.
- Efficiency: Store LOF results if used multiple times to avoid repeated calls.
- Binary Mode: For accurate sizes, open files in Binary mode.
- Cleanup: Always close files after using LOF to free resources.
This tutorial covered the FreeBasic LOF
function with practical
examples showing its usage in different file operations.
Author
List all FreeBasic Tutorials.