ZetCode

FreeBasic File Input and Output

Last modified October 18, 2026

A program reads and writes files with the Open statement. Each open file is given a file number, and statements such as Print #, Line Input #, and Close use that number. This article writes a text file, reads it back, appends to it, stores a binary value, checks for errors, and removes the files. The examples run in order, because each one uses the file the previous one created. For the individual statements, see the Open, Close, Line Input, and EOF pages.

Writing a text file

A text file is written by opening it For Output and sending lines to it with Print #. Opening an existing file this way replaces its contents. The example creates a file with three lines and closes it so the data is flushed to disk.

file_write.bas
Dim f As Integer = FreeFile
Open "notes.txt" For Output As #f
Print #f, "First line"
Print #f, "Second line"
Print #f, "Third line"
Close #f
Print "File written"

FreeFile returns a free file number, which is stored in f. Open "notes.txt" For Output As #f creates the file, or replaces it when it already exists. Each Print #f writes one line. Close #f flushes the data and releases the file number.

$ fbc -lang fb file_write.bas
$ ./file_write
File written

Reading a text file

Reading a text file uses For Input and a loop that runs until the end of the file. Line Input # returns one complete line at a time, so the program can count and print the lines as it goes. The example reads the file created in the previous section.

file_read.bas
Dim f As Integer = FreeFile
Dim lineText As String
Dim count As Integer = 0

Open "notes.txt" For Input As #f
Do Until EOF(f)
    Line Input #f, lineText
    count += 1
    Print count; ": "; lineText
Loop
Close #f

The file is opened For Input. Line Input #f, lineText reads one whole line, including any commas, into lineText. The loop continues until EOF(f) returns true, which means no more data remains. The program must run after file_write, which creates notes.txt.

$ fbc -lang fb file_read.bas
$ ./file_read
 1: First line
 2: Second line
 3: Third line

Appending to a file

For Append opens an existing file and writes at its end without erasing the contents.

file_append.bas
Dim f As Integer = FreeFile
Open "notes.txt" For Append As #f
Print #f, "Fourth line"
Close #f

Open "notes.txt" For Input As #f
Dim lineText As String
Dim total As Integer = 0
Do Until EOF(f)
    Line Input #f, lineText
    total += 1
Loop
Close #f
Print "Total lines now: "; total

The new line is added after the three lines already in the file. The second part counts all lines by reading the file again, so the total is 4.

$ fbc -lang fb file_append.bas
$ ./file_append
Total lines now:  4

Binary files

A binary file stores raw values rather than text lines. Put writes a value and Get reads it back. Both take the file number, an optional position, and the variable. The empty position in these statements means the operation continues from the current position.

file_binary.bas
Dim f As Integer = FreeFile
Dim outValue As Integer = 12345
Dim inValue As Integer

Open "data.bin" For Binary As #f
Put #f, , outValue
Close #f

Open "data.bin" For Binary As #f
Get #f, , inValue
Close #f
Print "Read back: "; inValue

outValue is written as the bytes of an Integer and then read into inValue. On a 64-bit build an Integer occupies eight bytes, so the file size depends on the target; the same program reads back the same value on that build. Because the file is reopened before reading, the read starts at the beginning of the file and returns 12345.

$ fbc -lang fb file_binary.bas
$ ./file_binary
Read back:  12345

Checking for errors

When the program must react to a failed open, use the function form Open(...). It returns 0 on success and a nonzero error code otherwise, so the program can report the problem and continue. The statement form does not return a code, so it cannot be checked this way. The code 2 means the file was not found.

file_errors.bas
Dim f As Integer = FreeFile
Dim result As Integer

result = Open("missing.txt" For Input As #f)
If result <> 0 Then
    Print "Could not open file, error code "; result
Else
    Close #f
End If

result = Open("missing.txt" For Input As #f) calls Open as a function. The file does not exist, so the result is 2 and the message reports that code. When the result is 0, the file is open and the program closes it.

$ fbc -lang fb file_errors.bas
$ ./file_errors
Could not open file, error code  2

Removing files

Kill deletes a file. The example deletes the two files created earlier, so running the article's programs again starts from a clean state.

file_cleanup.bas
Kill "notes.txt"
Kill "data.bin"
Print "Temporary files removed"

Kill "notes.txt" and Kill "data.bin" remove the files from the current directory. Deleting a file is permanent, so only use Kill on files the program created.

$ fbc -lang fb file_cleanup.bas
$ ./file_cleanup
Temporary files removed

Source

FreeBASIC documentation

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.