ZetCode

FreeBasic EOF Keyword

last modified June 16, 2025

The FreeBasic EOF function checks if the end of a file has been reached during reading operations. It's essential for safe file handling and prevents reading past the file's end.

Basic Definition

EOF stands for "End Of File". In FreeBasic, it's a function that returns a Boolean value indicating whether the end of a file has been reached.

The function takes a file handle as its parameter and returns -1 (true) if the end is reached, or 0 (false) otherwise. It's commonly used in loops that read file contents.

Reading a Text File Line by Line

This example demonstrates reading a text file until EOF is reached.

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

Dim line As String

While Not EOF(fileNum)
    Line Input #fileNum, line
    Print line
Wend

Close #fileNum

Here we open a file for reading and use a While loop with EOF as the condition. The loop continues until EOF returns true. Each iteration reads one line from the file and prints it.

Reading Binary Data Until EOF

EOF is also useful when reading binary files where data size isn't known.

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

Dim buffer As Byte

While Not EOF(fileNum)
    Get #fileNum, , buffer
    Print Hex(buffer); " ";
Wend

Close #fileNum

This code reads a binary file byte by byte until EOF. The Get statement reads each byte into the buffer variable. We print each byte's hexadecimal representation.

Combining EOF with Other File Functions

EOF can be combined with other file functions for more complex operations.

eof_combined.bas
Function CountLines(filename As String) As Integer
    Dim fileNum As Integer = FreeFile()
    Open filename For Input As #fileNum
    
    Dim count As Integer = 0
    Dim line As String
    
    While Not EOF(fileNum)
        Line Input #fileNum, line
        count += 1
    Wend
    
    Close #fileNum
    Return count
End Function

Print "Lines: "; CountLines("example.txt")

This function counts lines in a file using EOF to control the loop. Each iteration reads a line and increments the counter. The function returns the total line count when EOF is reached.

Handling Empty Files

EOF is particularly important when dealing with potentially empty files.

eof_empty.bas
Dim fileNum As Integer = FreeFile()
Open "empty.txt" For Input As #fileNum

If EOF(fileNum) Then
    Print "File is empty"
Else
    Print "File contains data"
End If

Close #fileNum

This example checks if a file is empty by testing EOF immediately after opening. If EOF is true right away, the file contains no data. This prevents attempts to read from empty files.

Reading Fixed-Length Records

EOF helps when reading files with fixed-length records or structures.

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

Dim fileNum As Integer = FreeFile()
Open "people.dat" For Binary As #fileNum

Dim p As Person

While Not EOF(fileNum)
    Get #fileNum, , p
    Print "Name: "; p.name; ", Age: "; p.age
Wend

Close #fileNum

Here we read a binary file containing fixed-size Person records. The loop continues until EOF, safely reading each complete structure. Without EOF, we might attempt to read partial or non-existent records.

Error Handling with EOF

Proper error handling should accompany EOF checks for robust file operations.

eof_error.bas
Dim fileNum As Integer = FreeFile()

If Open("missing.txt" For Input As #fileNum) <> 0 Then
    Print "Error opening file"
    End
End If

While Not EOF(fileNum)
    Dim line As String
    Line Input #fileNum, line
    Print line
Wend

Close #fileNum

This example shows basic error handling with EOF. We first check if the file opened successfully before attempting to read. The EOF loop then safely processes the file contents if opening succeeded.

Reading CSV Files

EOF is essential when processing CSV files where the record count varies.

eof_csv.bas
Dim fileNum As Integer = FreeFile()
Open "data.csv" For Input As #fileNum

Dim fields() As String
Dim line As String

While Not EOF(fileNum)
    Line Input #fileNum, line
    fields = Split(line, ",")
    
    For i As Integer = LBound(fields) To UBound(fields)
        Print fields(i); " | ";
    Next
    Print
Wend

Close #fileNum

This code reads a CSV file line by line until EOF. Each line is split into fields using the comma delimiter. The EOF check ensures we process all records regardless of how many exist in the file.

Best Practices

This tutorial covered the FreeBasic EOF function 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.