INPUT (file mode)

From QB64 Wiki
Redirect page
Jump to navigation Jump to search

The INPUT file mode in an OPEN statement opens an existing file for INPUT.


OPEN fileName$ FOR INPUT AS #filenumber%

  • If fileName$ does not exist, attempting to open it FOR INPUT will create a program file error. Use _FILEEXISTS to avoid errors.
  • The file number can be determined automatically by using a FREEFILE variable value.
  • Mode can use INPUT #, LINE INPUT # or INPUT$ to read the file data.
  • Use the EOF function to avoid reading data past the end of a file and creating an INPUT error.
  • 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 will work faster in BINARY than INPUT mode in QB64 to stay compatible with QBasic.


Example: Avoiding an INPUT mode or INPUT # 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.

See also

Keyword Reference - Alphabetical
Keyword Reference - By Usage
Main Wiki Page