INPUT (file mode) - QB64 Wiki

INPUT (file mode)

From QB64 Wiki

Jump to: navigation, search

The INPUT file mode can only OPEN existing files with data in them for program INPUT.


OPEN filename$ FOR INPUT AS #filenumber%

  • If the filename does not exist, INPUT will create a program file error! Use _FILEEXISTS in QB64 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 filenumber 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 QB.

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 FileExist%(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 FUNCTION FileExist% (filename$) f% = FREEFILE OPEN filename$ FOR APPEND AS #f% ' check that file exists IF LOF(f%) THEN FileExist% = -1 ELSE CLOSE #f%: KILL filename$ CLOSE #f% END FUNCTION

Explanation: The function opens the filename in APPEND mode to see if there is data in the file. It also creates the file if it did not exist. LOF will return 0 if the file is empty and cannot be read. In fact you can KILL the file if it is empty. If it is not empty then the function returns -1 and the existing file can be opened for INPUT and read by the program. _FILEEXISTS doesn't create any files.

See also:

Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page
Personal tools