Q(uick)BASIC Statement: LINE INPUT #

Quick View

LINE INPUT #

A file I/O statement that reads an entire line without delimiters from a sequential file to a string variable

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
  • LINE INPUT #filenumber,stringvariable
Description/Parameter(s)

The filenumber is the number used to open the file. The stringvariable is the variable the line is assigned to.

The LINE INPUT # statement reads all characters in the sequential file up to a carriage return. It then skips over the carriage-return and line-feed sequence. The next LINE INPUT # reads all characters up to the next carriage return.

LINE INPUT # is especially useful if each line of a data file has been broken into fields or a text file is being read a line at a time.

Example

This example uses LINE INPUT # to echo data input to a file:

'*** Programming example for LINE INPUT # statement*** OPEN "LIST" FOR OUTPUT AS #1 PRINT "CUSTOMER INFORMATION:" ' Get customer information. DO PRINT INPUT " LAST NAME: ", LName$ INPUT " FIRST NAME: ", FrName$ INPUT " AGE: ", Age$ INPUT " SEX: ", Sex$ Sex$=UCASE$(Sex$) WRITE #1, LName$, FrName$, Age$, Sex$ INPUT "Add another"; R$ LOOP WHILE UCASE$(R$)="Y" CLOSE #1 ' Echo the file back. OPEN "LIST" FOR INPUT AS #1 CLS PRINT "Records in file:" : PRINT DO WHILE NOT EOF(1) LINE INPUT #1, REC$ 'Read records from file with PRINT REC$ 'LINE INPUT #. Print the 'records on the screen. LOOP

Sample Output:

CUSTOMER INFORMATION: LAST NAME: Saintsbury FIRST NAME: Aloysius AGE: 35 SEX: m Add another? y LAST NAME: Frangio FIRST NAME: Louisa AGE: 27 SEX: f Add another? n Records in file: "Saintsbury","Aloysius","35","M" "Frangio","Louisa","27","F"
Syntax
  • LINE INPUT #filenumber%, stringvariable$
Description/Parameter(s)

Usage Notes

  • The LINE INPUT # statement reads all characters in the sequential file up to a carriage return. It then skips over the carriage-return and line-feed sequence.

Programming Tips

  • LINE INPUT # is especially useful if a text file is being read a line at a time.
Example

This example uses the WRITE # statement to write customer information to a data file. The LINE INPUT # statement is then used to read the records from the file.

OPEN "LIST" FOR OUTPUT AS #1 PRINT "CUSTOMER INFORMATION:" 'Get customer information. DO PRINT INPUT " LAST NAME: ", LName$ INPUT " FIRST NAME: ", FrName$ INPUT " AGE: ", Age$ INPUT " SEX: ", Sex$ Sex$ = UCASE$(Sex$) WRITE #1, LName$, FrName$, Age$, Sex$ INPUT "Add another"; R$ LOOP WHILE UCASE$(R$) = "Y" CLOSE #1 'Echo the file back. OPEN "LIST" FOR INPUT AS #1 CLS PRINT "Records in file:": PRINT DO WHILE NOT EOF(1) LINE INPUT #1, REC$ 'Read records from file. PRINT REC$ 'Print the records on the screen. LOOP 'Remove file from disk. CLOSE #1 KILL "LIST"

Sample Output:

CUSTOMER INFORMATION: LAST NAME: Saintsbury FIRST NAME: Aloysius AGE: 35 SEX: m Add another? y LAST NAME: Frangio FIRST NAME: Louisa AGE: 27 SEX: f Add another? n Records in file: "Saintsbury","Aloysius","35","M" "Frangio","Louisa","27","F"