FreeFile
Returns a free file number
Syntax
`
Declare Function FreeFile ( ) As Integer
`
Usage
`
result = FreeFile
`
Return Value
The next available file number.
Description
Returns the number of the next free file number. This value is a required argument to `Open` a file. `FreeFile` is useful when opening files in complex programs where the programmer can't keep track of the used file numbers.
Example
`
' Create a string and fill it.
Dim buffer As String, f As Integer
buffer = "Hello World within a file."
' Find the first free file number.
f = FreeFile
' Open the file "file.ext" for binary usage, using the file number "f".
Open "file.ext" For Binary As #f
' Place our string inside the file, using file number "f".
Put #f, , buffer
' Close the file.
Close
' End the program. (Check the file "file.ext" upon running to see the output.)
End
`
When using multiple `FreeFile` statements, `FreeFile` should be used immediately before the `Open` statement:
`
Dim fr As Integer, fs As Integer
' The CORRECT WAY:
fr = FreeFile
Open "File1" For Input As #fr
fs = FreeFile
Open "file2" For Input As #fs
`
As opposed to:
`
Dim fr As Integer, fs As Integer
' The WRONG way:
fr = FreeFile
fs = FreeFile
Open "file1" For Input As #fr
Open "file2" For Input As #fs
`
Platform Differences

- On Windows, a file number used in a dynamic link library is not the same as an identical file number used in the main program. File numbers can not be passed or returned and then used between a DLL and an executable.

Differences from QB

- None

See also

- `Open`
- `Put (File I/O)`
- `Get (File I/O)`