Q(uick)BASIC Function: FREEFILE
Quick View
FREEFILE
A file I/O function that returns the next free BASIC file number
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Description/Parameter(s)
FREEFILE returns the next valid unused file number.
Example
OPEN "TEST.DAT" FOR OUTPUT AS #1
PRINT "Next file number: "; FREEFILE
CLOSE
See also:
Syntax
- FREEFILE
Description/Parameter(s)
The FREEFILE function returns the next valid unused file number.
You can use this function to avoid having SUB or FUNCTION procedures use file numbers that are already in use.
Example
The example below uses FREEFILE to obtain a file number for opening a file:
INPUT "Enter file name ", Filename$
Filenum = FREEFILE
OPEN Filename$ FOR OUTPUT AS Filenum
PRINT Filename$;" opened as File #"; Filenum
Sample Output:
Enter file name: DATA.DAT DATA.DAT opened as File # 1See also:
Syntax
- FREEFILE
Description/Parameter(s)
Usage Notes
- Use FREEFILE when you need to supply a file number and you want to ensure that the number is not already in use.
Example
This example uses FREEFILE to obtain the next available file number. It uses the OPEN statement to open a sequential file, the CLOSE statement to close the file, and the KILL statement to delete the file from the disk.
DIM Filenum AS INTEGER
CLS
INPUT "Enter file name: ", filename$
Filenum% = FREEFILE
OPEN filename$ FOR OUTPUT AS Filenum%
PRINT : PRINT UCASE$(filename$); " opened for output as File #"; Filenum%
'Put something in the file.
PRINT #Filenum%, "The quick brown fox jumped over the lazy yellow dog."
'Close the file.
CLOSE Filenum%
Filenum% = FREEFILE
OPEN filename$ FOR INPUT AS Filenum%
PRINT : PRINT UCASE$(filename$); " has been reopened for input."
LINE INPUT #Filenum%, L$
PRINT : PRINT "The contents of the file are:"; L$
CLOSE Filenum%
'Remove the file from disk.
KILL filename$
See also: