LINE INPUT (file statement): Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(Created page with "The '''LINE INPUT #''' file statement reads an entire line from a text file into a string variable. {{PageSyntax}} : '''LINE INPUT''' '''#'''{{Parameter|fileNumber&}}''',''' ''stringVariable$'' {{Parameters}} * {{Parameter|fileNumber&}} is the INTEGER number of the file previously opened with the OPEN statement. * {{Parameter|stringVariable$}} holds the text line read from the file. {{PageDescription}} * Reads a file using the {{Parameter|fileNumber&}} OP...")
 
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 6: Line 6:




{{Parameters}}
{{PageParameters}}
* {{Parameter|fileNumber&}} is the [[INTEGER]] number of the file previously opened with the [[OPEN]] statement.
* {{Parameter|fileNumber&}} is the [[INTEGER]] number of the file previously opened with the [[OPEN]] statement.
* {{Parameter|stringVariable$}} holds the text line read from the file.
* {{Parameter|stringVariable$}} holds the text line read from the file.
Line 20: Line 20:
* '''Note: QB64''' will not remove CHR$(0) from the end of '''LINE INPUT #''' string return values like QBasic did.
* '''Note: QB64''' will not remove CHR$(0) from the end of '''LINE INPUT #''' string return values like QBasic did.


 
=== Errors ===
{{PageErrors}}
* '''If an "Input past End of file" error occurs, check for CHR$(26) (end of file character) in the files being read.'''
* '''If an "Input past End of file" error occurs, check for CHR$(26) (end of file character) in the files being read.'''
* '''Warning: files must exist to be opened in '''INPUT''' mode. Use [[_FILEEXISTS]] to avoid program [[ERROR Codes|errors]].'''
* '''Warning: files must exist to be opened in '''INPUT''' mode. Use [[_FILEEXISTS]] to avoid program [[ERROR Codes|errors]].'''
Line 28: Line 27:
{{PageExamples}}
{{PageExamples}}
''Example:'' Finding the number of filenames listed in a file to dimension an array to hold them.
''Example:'' Finding the number of filenames listed in a file to dimension an array to hold them.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|REDIM}} FileArray$(100) 'create {{Cl|$DYNAMIC|dynamic}} array
{{Cl|REDIM}} FileArray$(100) 'create {{Cl|$DYNAMIC|dynamic}} array
{{Cl|SHELL}} {{Cl|_HIDE}} "DIR /B *.* > D0S-DATA.INF"
{{Cl|SHELL}} {{Cl|_HIDE}} "DIR /B *.* > D0S-DATA.INF"
{{Cl|IF...THEN|IF}} {{Cl|_FILEEXISTS}}("D0S-DATA.INF") THEN  
{{Cl|IF...THEN|IF}} {{Cl|_FILEEXISTS}}("D0S-DATA.INF") THEN
   {{Cl|OPEN}} "D0S-DATA.INF" FOR {{Cl|INPUT (file mode)|INPUT}} AS #1  
   {{Cl|OPEN}} "D0S-DATA.INF" FOR {{Cl|INPUT (file mode)|INPUT}} AS #1
   DO UNTIL {{Cl|EOF}}(1)
   DO UNTIL {{Cl|EOF}}(1)
     {{Cl|LINE INPUT}} #1, file$        'read entire text file line
     {{Cl|LINE INPUT}} #1, file$        'read entire text file line
Line 40: Line 39:
{{Cl|END IF}}
{{Cl|END IF}}
{{Cl|REDIM}} FileArray$(filecount%)
{{Cl|REDIM}} FileArray$(filecount%)
{{Cl|PRINT}} filecount% '' ''
{{Cl|PRINT}} filecount%
{{CodeEnd}}
{{CodeEnd}}


Line 46: Line 45:
{{PageSeeAlso}}
{{PageSeeAlso}}
* [[OPEN]], [[CLOSE]]
* [[OPEN]], [[CLOSE]]
* [[INPUT (file mode)]], [[INPUT (file statement)|INPUT #]], [[INPUT$]] {{text|(file input)}}
* [[INPUT (file mode)]], [[INPUT (file statement)|INPUT #]], [[INPUT$]] {{Text|(file input)}}
* [[INPUT]], [[LINE INPUT]], [[INPUT$]] {{text|(keyboard input)}}
* [[INPUT]], [[LINE INPUT]], [[INPUT$]] {{Text|(keyboard input)}}
* [[_FILEEXISTS]], [[_DIREXISTS]]
* [[_FILEEXISTS]], [[_DIREXISTS]]
* [[FILELIST$]] (member-contributed function replacement for [[FILES]])
* [[FILELIST$]] (member-contributed function replacement for [[FILES]])

Latest revision as of 22:35, 11 February 2023

The LINE INPUT # file statement reads an entire line from a text file into a string variable.


Syntax

LINE INPUT #fileNumber&, stringVariable$


Parameters

  • fileNumber& is the INTEGER number of the file previously opened with the OPEN statement.
  • stringVariable$ holds the text line read from the file.


Description

  • Reads a file using the fileNumber& OPENed in the INPUT (file mode) or BINARY file mode as one file line text string.
  • NOTE: LINE INPUT will work faster in BINARY mode than in INPUT mode.
    • Using LINE INPUT # in BINARY mode is possible in version 1.000 and up
  • Can be used with EOF to count the number of lines of data (records) in a file using a loop.
  • Use the EOF function to avoid going past the end of a file and creating an error.
  • LINE INPUT # can even retain the original quotation marks in text.
  • Note: QB64 will not remove CHR$(0) from the end of LINE INPUT # string return values like QBasic did.

Errors

  • If an "Input past End of file" error occurs, check for CHR$(26) (end of file character) in the files being read.
  • Warning: files must exist to be opened in INPUT mode. Use _FILEEXISTS to avoid program errors.


Examples

Example: Finding the number of filenames listed in a file to dimension an array to hold them.

REDIM FileArray$(100) 'create dynamic array
SHELL _HIDE "DIR /B *.* > D0S-DATA.INF"
IF _FILEEXISTS("D0S-DATA.INF") THEN
  OPEN "D0S-DATA.INF" FOR INPUT AS #1
  DO UNTIL EOF(1)
    LINE INPUT #1, file$        'read entire text file line
    filecount% = filecount% + 1
  LOOP
  CLOSE #1
END IF
REDIM FileArray$(filecount%)
PRINT filecount%


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage