FreeBasic Open Keyword
last modified June 16, 2025
The FreeBasic Open
keyword is used for file operations. It
allows reading from and writing to files on disk. File handling is essential
for persistent data storage in applications.
Basic Definition
In FreeBasic, Open
creates a connection between a program and
a file. It specifies the file name, access mode, and file number. The file
number is used to reference the opened file in subsequent operations.
The Open
statement must be paired with a Close
statement. Files can be opened for reading, writing, or both. Different
modes control how the file is accessed and whether it's created if missing.
Opening a File for Reading
This example shows how to open a file for reading text data.
Dim fileNum As Integer = FreeFile() Open "data.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 "data.txt" for input (reading). FreeFile()
gets
an available file number. We read lines until EOF (end-of-file) is reached.
Each line is printed to the console. Finally, we close the file.
Opening a File for Writing
This example demonstrates opening a file for writing new content.
Dim fileNum As Integer = FreeFile() Open "output.txt" For Output As #fileNum Print #fileNum, "First line of text" Print #fileNum, "Second line of text" Close #fileNum
We open "output.txt" for output (writing). If the file exists, it's
truncated. If it doesn't exist, it's created. We write two lines using
Print #
and close the file. This is useful for creating logs.
Appending to a File
This example shows how to append data to an existing file.
Dim fileNum As Integer = FreeFile() Open "log.txt" For Append As #fileNum Print #fileNum, "New log entry: "; Date() Print #fileNum, "---------------------" Close #fileNum
Using For Append
mode preserves existing content. New data is
added at the end. This is ideal for log files or accumulating data. The
Date()
function adds a timestamp to our log entry.
Binary File Access
Binary mode allows reading and writing raw bytes to a file.
Dim fileNum As Integer = FreeFile() Open "data.bin" For Binary As #fileNum Dim buffer As String * 10 Get #fileNum, , buffer Put #fileNum, , "NewData1234" Close #fileNum
Binary mode is used for non-text files or precise byte control. Get
reads bytes into a buffer. Put
writes bytes from a string. The
position can be specified or left to advance sequentially.
Random Access Files
Random access allows reading/writing fixed-length records at any position.
Type Person name As String * 20 age As Integer End Type Dim fileNum As Integer = FreeFile() Open "people.dat" For Random As #fileNum Len = Len(Person) Dim p As Person p.name = "John Doe" p.age = 30 Put #fileNum, 1, p Get #fileNum, 1, p Print "Name: "; p.name; ", Age: "; p.age Close #fileNum
Random access files use fixed-length records. We define a Type
structure. Len = Len(Person)
sets the record size. Records
are accessed by number (1-based). This is efficient for database-like files.
Checking File Existence Before Opening
It's good practice to check if a file exists before opening it.
Dim filename As String = "config.ini" If Dir(filename) = "" Then Print "File not found, creating new one" Dim fileNum As Integer = FreeFile() Open filename For Output As #fileNum Print #fileNum, "[settings]" Close #fileNum Else Print "File exists, opening for reading" Dim fileNum As Integer = FreeFile() Open filename For Input As #fileNum ' Read file contents... Close #fileNum End If
Dir()
checks if a file exists. If it doesn't, we create it.
If it exists, we open it for reading. This prevents errors when opening
non-existent files. Always handle both cases in file operations.
File Locking for Multi-Process Access
File locking prevents conflicts when multiple processes access a file.
Dim fileNum As Integer = FreeFile() Open "shared.txt" For Binary Access Read Write Lock Read Write As #fileNum ' Exclusive access granted Print #fileNum, "Process 1 was here" Sleep 2000 ' Simulate work Close #fileNum
The Lock
clause controls access. Lock Read Write
requests exclusive access. Other processes will wait until the file is
unlocked. This is crucial for multi-process applications sharing files.
Best Practices
- Error Handling: Always check for file operation success.
- Resource Management: Close files promptly when done.
- File Numbers: Use
FreeFile()
to avoid conflicts. - Modes: Choose the appropriate access mode for your needs.
- Paths: Use absolute paths or handle relative paths carefully.
This tutorial covered the FreeBasic Open
keyword with practical
examples showing file operations in different scenarios.
Author
List all FreeBasic Tutorials.