Revision [14860]

This is an old revision of KeyPgFreefile made by DoS386 on 2010-10-12 02:05:43.



Returns a free file number

result = Freefile

Return Value:
The next available file number.

Returns the number of the next free file number with valid values 1 to 254, or ZERO if there are already 254 files opened. This value is a required argument to KeyPgOpen Open a file. Freefile is useful when opening files in complex programs where the programmer can't keep track of the used file numbers. Make sure to always close files when no longer needed, otherwise you will get a filenumber leak, and won't be able to open any files anymore after 254 filenumbers are exhausted while your program is running. All opened files will get closed automatically at program termination.

' 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.

' End the program. (Check the file "file.ext" upon running to see the output.)

When using multiple Freefile statements, Freefile should be used immediately before the KeyPgOpen Open statement:
Dim fr As Integer, fs As Integer
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:
Differences from QB:
See also:
Back to File I/O Functions
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki phatcode