The INPUT file mode in an OPEN statement opens an existing file for INPUT (file statement).
OPEN fileName$ FOR INPUT AS #filenumber%
- If fileName$ does not exist, attempting to open it FOR INPUT will create a program ERROR Codes. Use _FILEEXISTS to avoid errors.
- The file number can be determined automatically by using a FREEFILE variable value.
- Mode can use INPUT (file statement) #, LINE INPUT (file statement) # or INPUT$ to read the file data.
- Use the EOF function to avoid reading data past the end of a file and creating an ERROR Codes.
- Input file statements will use the same file number as the OPEN statement.
- The INPUT mode allows the same file to be opened in another mode with a different number.
- NOTE: LINE INPUT (file statement) will work faster in BINARY than INPUT mode in QB64 to stay compatible with QBasic.
Avoiding an INPUT mode or INPUT (file statement) read error using a FileExist function. QB64 can use the _FILEEXISTS function.
DIM Fdata$(100) INPUT "Enter data file name: ", datafile$ IF _FILEEXISTS(datafile$) THEN D% = FREEFILE: count = 0 OPEN datafile$ FOR INPUT AS #D% DO UNTIL EOF(D%) count = count + 1 LINE INPUT #D%, Fdata$(count) IF count = 100 THEN EXIT DO ' don't exceed array size! LOOP CLOSE #D% ELSE : PRINT "File not found!" END IF
Explanation: The _FILEEXISTS function is used before
OPEN datafile$ FOR INPUT AS #D%, which would generate an error in case the file didn’t exist.
- INPUT (file statement), LINE INPUT (file statement), INPUT$ (file input)
- INPUT, LINE INPUT, INPUT$ (keyboard input)
- APPEND, RANDOM, OUTPUT, BINARY
- READ, DATA
- _FILEEXISTS, _DIREXISTS