INPUT (file statement) - QB64 Wiki

INPUT (file statement)

From QB64 Wiki

Jump to: navigation, search

The INPUT # file or port statement reads sequential data using one variable or a comma separated list of matching variable types.


Syntax:

INPUT #filenumber&, variable[, listof!, variables$,...]


Parameters:

  • filenumber is a positive LONG integer value used to OPEN the file FOR INPUT mode only.
  • The type of the variable define the value or list of values to be returned from the file. Numeric types must match the values returned.
  • As reflected in the syntax you can list a number of variables with different types seperated by a comma and they will hold the values in the file (keep in mind that the information in the file should reflect the variable types used).


Usage:

  • The file number can be determined by the programmer or an unused number can be returned by the FREEFILE function.
  • INPUT # reads file data from a filenumber& OPENed in the INPUT file mode.
  • INPUT # can read one variable at a time from a list or read the entire list by comma separating a list of input variables.
  • Variable types MUST match the numerical types being read! STRING variables can return numeric values not in quotes.
  • Leading or trailing spaces of STRING values must be inside of quotes. WRITE # writes strings inside of quotes automatically. PRINT # removes quotes.
  • INPUT # will read each value until it encounters a comma for the next value in a list.
  • Use the EOF function to avoid reading past the end of a file.
  • Files created by WRITE # usually have the same number of values on each file line. If INPUT reads more or less values, it may read beyond the End of File or return bad data input!
  • Use the LINE INPUT (file statement) for files created by PRINT # or PRINT #, USING.
  • INPUT can read Excel CSV files, but beware of unquoted text or numerical values containing commas!


Example 1: Writes new data to a text file sequentially and reads it back to the program screen.

filename$ = "testfile.dat" x = 1: y = 2: z$ = "Three" OPEN filename$ FOR OUTPUT AS #1 'opens and clears an existing file or creates new empty file WRITE #1, x, y, z$ CLOSE #1 PRINT "File created with data. Press a key!" K$ = INPUT$(1) 'press a key OPEN filename$ FOR INPUT AS #2 'opens a file to read it INPUT #2, a, b, c$ CLOSE #2 PRINT a, b, c$ WRITE a, b, c$ END

1 2 Three 1,2,"Three"

Screen output: PRINT string values will not display enclosing quotes. WRITE screen displays will.

1,2,"Three"

File content: WRITE string values will include quotation marks, but they are not required to read the file value as a string.


Example 2: Commas inside of string values will not affect the INPUT value as those commas are not WRITE separators.

x$ = "Hello, how are you?" y$ = "I'm fine." OPEN "testinp.dat" FOR OUTPUT AS #1 WRITE #1, x$, y$ CLOSE #1 OPEN "testinp.dat" FOR INPUT AS #1 INPUT #1, a$, b$ CLOSE #1 PRINT a$, b$ WRITE a$, b$

Hello, how are you? I'm fine. "Hello, how are you?","I'm fine."

"Hello, how are you?","I'm fine."

File content: Commas inside of strings delimited with quotes will be ignored. WRITE will always enclose string values in quotes.


See also:



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