ZetCode

FreeBasic Close Keyword

last modified June 16, 2025

The FreeBasic Close keyword is used to close open files and free system resources. It's an essential part of proper file handling in FreeBasic. Files should always be closed when no longer needed.

Basic Definition

In FreeBasic, Close is a statement that terminates access to a previously opened file. It ensures all buffered data is written and releases the file handle back to the operating system.

The Close statement can work with a specific file number or close all open files when used without parameters. Proper file closing prevents data loss and resource leaks.

Closing a Single File

This example demonstrates basic file operations with explicit closing.

close_basic.bas
Dim fileNum As Integer = FreeFile()
Open "data.txt" For Output As #fileNum

Print #fileNum, "Hello, FreeBasic!"
Close #fileNum

Print "File written and closed successfully"

Here we open a file for writing, write some text, then explicitly close it using the file number. The FreeFile function gets an available file number. Always closing files is a good programming practice.

Closing All Open Files

The Close statement can close all open files at once.

close_all.bas
Open "file1.txt" For Output As #1
Open "file2.txt" For Output As #2
Open "file3.txt" For Output As #3

Print #1, "File 1 content"
Print #2, "File 2 content"
Print #3, "File 3 content"

Close ' Closes all open files

Print "All files closed"

This example opens three files and writes to each. The parameterless Close statement closes all open files simultaneously. This is useful when you need to ensure all files are properly closed.

Close in Error Handling

Files should be closed even when errors occur during file operations.

close_error.bas
Dim fileNum As Integer = FreeFile()

On Error Goto ErrorHandler

Open "data.txt" For Input As #fileNum
Dim content As String
Line Input #fileNum, content
Print "File content: "; content

ErrorHandler:
If Err Then
    Print "Error: "; Error(Err)
    If fileNum Then Close #fileNum
    End
End If

Close #fileNum

This example shows proper file closing in an error handler. The file is closed whether an error occurs or not. The If fileNum check prevents errors if the file wasn't successfully opened before the error occurred.

Close with Binary Files

Binary files also need to be properly closed after operations.

close_binary.bas
Dim fileNum As Integer = FreeFile()
Dim buffer(0 To 99) As Byte

Open "data.bin" For Binary As #fileNum
Get #fileNum, , buffer()
Close #fileNum

Print "Read "; UBound(buffer) + 1; " bytes from binary file"

This code demonstrates reading from a binary file and closing it. Binary files handle raw data and require the same careful resource management as text files. The Close statement works identically for all file types.

Close in File Copy Operation

File copying requires proper handling of both source and destination files.

close_copy.bas
Dim srcFile As Integer = FreeFile()
Dim dstFile As Integer = FreeFile() + 1
Dim line As String

Open "source.txt" For Input As #srcFile
Open "destination.txt" For Output As #dstFile

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

Close #srcFile
Close #dstFile

Print "File copied successfully"

This example copies a file line by line. Both files are explicitly closed after the operation. Notice we use different file numbers for source and destination. Always close all files when done with them.

Close with Random Access Files

Random access files also need proper closing after use.

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

Dim p As Person
Dim fileNum As Integer = FreeFile()

Open "people.dat" For Random As #fileNum Len = Len(p)
p.name = "John Doe"
p.age = 30
Put #fileNum, 1, p
Close #fileNum

Print "Record written and file closed"

This example writes a record to a random access file and closes it. Random access files allow reading/writing at specific positions. The Close statement works the same way as with other file types.

Close in File Appending

Appending to files also requires proper closing.

close_append.bas
Dim fileNum As Integer = FreeFile()

Open "log.txt" For Append As #fileNum
Print #fileNum, "New log entry: "; Time
Close #fileNum

Print "Log entry appended and file closed"

This example demonstrates appending to a file and closing it. Append mode automatically seeks to the end of file before writing. The Close ensures the new content is properly saved to disk.

Best Practices

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