Q(uick)BASIC Statement: INPUT #
Quick View
INPUT #
A file I/O statement that reads data items from a sequential device or file and assigns them to variables
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- INPUT [;] ["prompt"{; | ,}] variablelist
- LINE INPUT [;] ["prompt";] variable$
- INPUT #filenumber%, variablelist
- LINE INPUT #filenumber%, variable$
Description/Parameter(s)
prompt | An optional literal string that is displayed before the user enters data. A semicolon after prompt appends a question mark to the prompt string. |
variablelist | One or more variables, separated by commas, in which data entered from the keyboard or read from a file is stored. Variable names can consist of up to 40 characters and must begin with a letter. Valid characters are A-Z, 0-9, and period (.). |
variable$ | Holds a line of characters entered from the keyboard or read from a file. |
filenumber% | The number of an open file. |
- INPUT uses a comma as a separator between entries. LINE INPUT reads all characters up to a carriage return.
- For keyboard input, a semicolon immediately after INPUT keeps the cursor on the same line after the user presses the Enter key.
Example
CLS
OPEN "LIST" FOR OUTPUT AS #1
DO
INPUT " NAME: ", Name$ 'Read entries from the keyboard.
INPUT " AGE: ", Age$
WRITE #1, Name$, Age$
INPUT "Add another entry"; R$
LOOP WHILE UCASE$(R$) = "Y"
CLOSE #1
'Echo the file back.
OPEN "LIST" FOR INPUT AS #1
CLS
PRINT "Entries in file:": PRINT
DO WHILE NOT EOF(1)
LINE INPUT #1, REC$ 'Read entries from the file.
PRINT REC$ 'Print the entries on the screen.
LOOP
CLOSE #1
KILL "LIST"
Syntax
- INPUT #filenumber, variablelist
Description/Parameter(s)
The filenumber is the number used when the file was opened for input. The variablelist contains the names of the variables that are assigned values read from the file. (The variable type must match the type specified by the variable name.)
The data items in the file should appear just as they would if you were entering data in response to an INPUT statement. Separate numbers with a space, carriage return, line feed, or comma. Separate strings with a carriage return or line feed (leading spaces are ignored). The end-of-file character will end either a numeric or string entry.
If BASIC is scanning the sequential data file for a string item, it will also ignore leading spaces, carriage returns, and line feeds. If end-of-file is reached when a numeric or string item is being INPUT, the item is terminated.
Example
This example reads a series of test scores from a sequential file and calculates the average score.
Note: Before you run this program, create a one-line data file CLASS.DAT made up of a series of test scores - 97 84 63 89 100.
DEFINT A-Z
CLS ' Clear screen
OPEN "class.dat" FOR INPUT AS #1
DO WHILE NOT EOF(1)
Count = Count + 1
INPUT #1, Score
Total = Total + Score
PRINT Count; Score
LOOP
PRINT
PRINT "Total students:";Count;" Average score:";Total / Count
END
Sample Output:
1 97 2 84 3 63 4 89 5 100See also:
Syntax
- INPUT #filenumber%, variablelist
Description/Parameter(s)
filenumber% | The number used in the OPEN statement to open the file. |
variablelist | One or more variable names, separated by commas, that will be assigned values read from the file. |
- The data items in the file should appear just as they would if you were entering data in response to an INPUT statement. Separate numbers with a space, carriage return, line feed, or comma. Separate strings with a carriage return or line feed (leading spaces are ignored). The end-of-file character will end either a numeric or string entry.
Example
This example uses the INPUT # statement to read a series of test scores from a sequential file, then calculates and displays the average score. The program uses the EOF function to determine when the last score has been read from the file.
DEFINT A-Z
DIM Total AS LONG
CLS
'Create the required input file.
OPEN "class.dat" FOR OUTPUT AS #1
PRINT #1, 98, 84, 63, 89, 100
CLOSE #1
'Open the input file just created.
OPEN "class.dat" FOR INPUT AS #1
DO WHILE NOT EOF(1) 'Do as long as the end-of-file hasn't been reached.
Count = Count + 1
INPUT #1, Score 'Get a score from file #1.
Total = Total + Score
PRINT Count; Score
LOOP
PRINT
PRINT "Total students:"; Count; " Average score:"; Total / Count
END
Sample Output:
1 98 2 84 3 63 4 89 5 100 Total students: 5 Average score: 86.8See also: