#REDIRECT [[OPEN#File_Access_Modes]]

The '''INPUT''' file mode in an [[OPEN]] statement opens an existing file for [[INPUT (file statement)|INPUT]].


{{PageSyntax}}
: [[OPEN]] {{Parameter|fileName$}} FOR '''INPUT''' AS #filenumber%


* If {{Parameter|fileName$}} does not exist, attempting to open it FOR INPUT will create a program [[ERROR Codes|file error]]. Use [[_FILEEXISTS]] to avoid errors.
* The file number can be determined automatically by using a [[FREEFILE]] variable value.
* Mode can use [[INPUT (file statement)|INPUT]] #, [[LINE INPUT (file statement)|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 [[ERROR Codes|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 (file statement)|LINE INPUT]] will work faster in [[BINARY]] than INPUT mode in QB64 to stay compatible with QBasic.'''


{{PageExamples}}
''Example:'' Avoiding an INPUT mode or [[INPUT (file statement)|INPUT #]] read error using a FileExist function. QB64 can use the [[_FILEEXISTS]] function.

{{CodeStart}} '' ''
 DIM Fdata$(100)
 INPUT "Enter data file name: ", datafile$ 
 IF _FILEEXISTS(datafile$) THEN
    D% = {{Cl|FREEFILE}}: count = 0
    {{Cl|OPEN}} datafile$ FOR {{Cl|INPUT (file mode)|INPUT}} AS #D%
    DO UNTIL {{Cl|EOF}}(D%)
     count = count + 1
     {{Cl|INPUT (file statement)|LINE INPUT}} #D%, Fdata$(count)
     IF count = 100 THEN {{Cl|EXIT}} DO  ' don't exceed {{Cl|Arrays|array}} size!
    LOOP
  {{Cl|CLOSE}} #D%
 ELSE : PRINT "File not found!"
 END IF
{{CodeEnd}}
: ''Explanation:'' The [[_FILEEXISTS]] function is used before {{InlineCode}}OPEN datafile$ FOR INPUT AS #D%{{InlineCodeEnd}}, which would generate an error in case the file didn't exist.


{{PageSeeAlso}}
* [[INPUT (file statement)|INPUT #]], [[LINE INPUT (file statement)|LINE INPUT #]], [[INPUT$]] {{text|(file input)}}
* [[INPUT]], [[LINE INPUT]], [[INPUT$]] {{text|(keyboard input)}}
* [[APPEND]], [[RANDOM]], [[OUTPUT]], [[BINARY]]
* [[READ]], [[DATA]]
* [[_FILEEXISTS]], [[_DIREXISTS]]


{{PageNavigation}}
