INPUT (file statement): Difference between revisions
Jump to navigation
Jump to search
Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link
m (Protected "INPUT (file statement)" ([Edit=Allow only autoconfirmed users] (indefinite) [Move=Allow only autoconfirmed users] (indefinite))) |
No edit summary |
||
(4 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
{{PageSyntax}} | {{PageSyntax}} | ||
: [[INPUT #]]{{Parameter|fileNumber&}}, {{Parameter|variable1}}[, {{Parameter|variable2}}, ..., {{Parameter|variableN}}] | : [[INPUT #]]{{Parameter|fileNumber&}}, {{Parameter|variable1}}[, {{Parameter|variable2}}, ..., {{Parameter|variableN}}] | ||
{{ | {{PageParameters}} | ||
* {{Parameter|fileNumber&}} is a positive [[LONG]] integer value used to [[OPEN]] the file FOR [[INPUT (file mode)|INPUT]] mode. | * {{Parameter|fileNumber&}} is a positive [[LONG]] integer value used to [[OPEN]] the file FOR [[INPUT (file mode)|INPUT]] mode. | ||
* The [[type]] of the ''variable'' used defines the value or list of values to be returned from the file. Numeric types must match the values returned. | * The [[Variable Types|type]] of the ''variable'' used defines 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 match the variable types used). | * 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 match the variable types used). | ||
Line 14: | Line 14: | ||
{{PageDescription}} | {{PageDescription}} | ||
* The file number can be determined by the programmer or be an unused number returned by the [[FREEFILE]] function. | * The file number can be determined by the programmer or be an unused number returned by the [[FREEFILE]] function. | ||
* Variable types must match the numerical [[type]]s being read. [[STRING]] variables can return unquoted numeric values. | * Variable types must match the numerical [[Variable Types|type]]s being read. [[STRING]] variables can return unquoted numeric values. | ||
* Leading or trailing spaces of [[STRING]] values must be inside of quotes. [[WRITE (file statement)|WRITE #]] writes strings inside of quotes automatically. [[PRINT (file statement)|PRINT #]] removes quotes. | * Leading or trailing spaces of [[STRING]] values must be inside of quotes. [[WRITE (file statement)|WRITE #]] writes strings inside of quotes automatically. [[PRINT (file statement)|PRINT #]] removes quotes. | ||
* [[INPUT #]] will read each value until it encounters a comma for the next value in a list. | * [[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. | * Use the [[EOF]] function to avoid reading past the end of a file. | ||
* Files created by [[WRITE (file statement)|WRITE #]] usually have the same number of values on each file line. If INPUT reads more or less values, it may read beyond the [[EOF|end of file]] or return bad data. | * Files created by [[WRITE (file statement)|WRITE #]] usually have the same number of values on each file line. If INPUT reads more or less values, it may read beyond the [[EOF|end of file]] or return bad data. | ||
* Use the [[LINE INPUT (file statement)]] for files created with PRINT # or PRINT #, USING. | * Use the [[LINE INPUT (file statement)]] for files created with PRINT # or PRINT #, USING. | ||
* '''INPUT can read Excel CSV files, but beware of unquoted text or numerical values containing commas.''' | * '''INPUT can read Excel CSV files, but beware of unquoted text or numerical values containing commas.''' | ||
Line 25: | Line 25: | ||
{{PageExamples}} | {{PageExamples}} | ||
''Example 1:'' Writes new data to a text file sequentially and reads it back to the program screen. | ''Example 1:'' Writes new data to a text file sequentially and reads it back to the program screen. | ||
{{CodeStart}} | {{CodeStart}} | ||
filename$ = "testfile.dat" | filename$ = "testfile.dat" | ||
x = 1: y = 2: z$ = "Three" | x = 1: y = 2: z$ = "Three" | ||
{{Cl|OPEN}} filename$ {{Cl|FOR...NEXT|FOR}} {{Cl|OUTPUT}} {{Cl|AS}} #1 'opens and clears an existing file or creates new empty file | {{Cl|OPEN}} filename$ {{Cl|FOR...NEXT|FOR}} {{Cl|OUTPUT}} {{Cl|AS}} #1 'opens and clears an existing file or creates new empty file | ||
{{Cl|WRITE (file statement)|WRITE}} #1, x, y, z$ | {{Cl|WRITE (file statement)|WRITE}} #1, x, y, z$ | ||
{{Cl|CLOSE}} #1 | {{Cl|CLOSE}} #1 | ||
{{Cl|PRINT}} "File created with data. Press a key!" | {{Cl|PRINT}} "File created with data. Press a key!" | ||
K$ = {{Cl|INPUT$}}(1) 'press a key | K$ = {{Cl|INPUT$}}(1) 'press a key | ||
{{Cl|OPEN}} filename$ {{Cl|FOR (file statement)|FOR}} {{Cl|INPUT (file mode)|INPUT}} {{Cl|AS}} #2 'opens a file to read it | {{Cl|OPEN}} filename$ {{Cl|FOR (file statement)|FOR}} {{Cl|INPUT (file mode)|INPUT}} {{Cl|AS}} #2 'opens a file to read it | ||
{{Cl|INPUT (file statement)|INPUT}} #2, a, b, c$ | {{Cl|INPUT (file statement)|INPUT}} #2, a, b, c$ | ||
{{Cl|CLOSE}} #2 | {{Cl|CLOSE}} #2 | ||
{{Cl|PRINT}} a, b, c$ | {{Cl|PRINT}} a, b, c$ | ||
{{Cl|WRITE}} a, b, c$ | {{Cl|WRITE}} a, b, c$ | ||
{{Cl|END}} | {{Cl|END}} | ||
{{CodeEnd}} | {{CodeEnd}} | ||
{{OutputStart}} 1 2 Three | {{OutputStart}} 1 2 Three | ||
Line 56: | Line 56: | ||
''Example 2:'' Commas inside of string values will not affect the INPUT value as those commas are not [[WRITE (file statement)|WRITE]] separators. | ''Example 2:'' Commas inside of string values will not affect the INPUT value as those commas are not [[WRITE (file statement)|WRITE]] separators. | ||
{{CodeStart}} | {{CodeStart}} | ||
x$ = "Hello, how are you?" | x$ = "Hello, how are you?" | ||
y$ = "I'm fine." | y$ = "I'm fine." | ||
Line 70: | Line 70: | ||
{{Cl|CLOSE}} #1 | {{Cl|CLOSE}} #1 | ||
{{Cl|PRINT}} a$, b$ | {{Cl|PRINT}} a$, b$ | ||
{{Cl|WRITE}} a$, b$ | {{Cl|WRITE}} a$, b$ | ||
{{CodeEnd}} | {{CodeEnd}} | ||
{{OutputStart}}Hello, how are you? I'm fine. | {{OutputStart}}Hello, how are you? I'm fine. | ||
"Hello, how are you?","I'm fine."{{OutputEnd}} | "Hello, how are you?","I'm fine."{{OutputEnd}} | ||
{{TextStart}}"Hello, how are you?","I'm fine."{{TextEnd}} | {{TextStart}}"Hello, how are you?","I'm fine."{{TextEnd}} | ||
Line 80: | Line 80: | ||
{{PageSeeAlso}} | {{PageSeeAlso}} | ||
* [[INPUT (file mode)]], [[LINE INPUT (file statement)|LINE INPUT #]], [[INPUT$]] {{ | * [[INPUT (file mode)]], [[LINE INPUT (file statement)|LINE INPUT #]], [[INPUT$]] {{Text|(file input)}} | ||
* [[INPUT]], [[LINE INPUT]], [[INPUT$]] {{ | * [[INPUT]], [[LINE INPUT]], [[INPUT$]] {{Text|(keyboard input)}} | ||
* [[PRINT (file statement)|PRINT #]], [[PRINT USING (file statement)|PRINT #, USING]] | * [[PRINT (file statement)|PRINT #]], [[PRINT USING (file statement)|PRINT #, USING]] | ||
* [[GET|GET #]] | * [[GET|GET #]] | ||
{{PageNavigation}} | {{PageNavigation}} |
Latest revision as of 20:25, 26 January 2024
The INPUT # file or port statement reads sequential data using one variable or a comma separated list of matching variable types.
Syntax
- INPUT #fileNumber&, variable1[, variable2, ..., variableN]
Parameters
- fileNumber& is a positive LONG integer value used to OPEN the file FOR INPUT mode.
- The type of the variable used defines 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 match the variable types used).
Description
- The file number can be determined by the programmer or be an unused number returned by the FREEFILE function.
- Variable types must match the numerical types being read. STRING variables can return unquoted numeric values.
- 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.
- Use the LINE INPUT (file statement) for files created with PRINT # or PRINT #, USING.
- INPUT can read Excel CSV files, but beware of unquoted text or numerical values containing commas.
Examples
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" |
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
- INPUT (file mode), LINE INPUT #, INPUT$ (file input)
- INPUT, LINE INPUT, INPUT$ (keyboard input)
- PRINT #, PRINT #, USING
- GET #