LINE INPUT statement (from file)

Purpose:

LINE INPUT reads a line of text data from a file.

Syntax:

LINE INPUT hFile AS HWND, Buffer AS STRING [, Limit AS INTEGER]

Parameters:

  • Data type: FILE
    hFile The handle of file from which line of text is read. File must be opened using
    OPEN FileName$ FOR INPUT AS hFile
    
    syntax.
  • Data type: STRING
    Buffer String to receive line of text.
  • Data type: INTEGER
    Limit Argument for specifying the memory limit of the receiving variable, including the required null terminator. For example:
    LINE INPUT FP1, A$, 5000
    
    will read up to, but no more than 4999 characters and add a null terminator.

Remarks:

👉 If the length of any line being read from the file is greater than the size, including any carriage return or new line indicators, of the receiving Buffer, default 2048 bytes, optionally specified in the Limit parameter, LINE INPUT will abort the application with a message identifying the line in the C translation at which the truncation was detected, as well as the truncated portion of the line of text that caused the error condition. The name of the file containing the too long line is also messaged, however, it is left up to the user to determine the exact location of the offending line in that file.

Example 1:

DIM FileName$
DIM z%

FileName$ = "Test.txt"

OPEN FileName$ FOR OUTPUT AS FP1
? "Creating test file ..."
FOR z% = 1 TO 10
 FPRINT FP1, "This is not the end. THIS IS THE END "; STR$(z%)
NEXT
CLOSE FP1


DIM DynBuffer$ * 42
OPEN FileName$ FOR INPUT AS FP2
WHILE NOT EOF(FP2)
 LINE INPUT FP2, DynBuffer$
 ? DynBuffer$
WEND
CLOSE FP2
?
REDIM DynBuffer$ * 22
OPEN FileName$ FOR INPUT AS FP2
WHILE NOT EOF(FP2)
 LINE INPUT FP2, DynBuffer$, 22
 ? DynBuffer$
WEND
CLOSE FP2

Result:

Creating test file ...
This is not the end. THIS IS THE END  1
This is not the end. THIS IS THE END  2
This is not the end. THIS IS THE END  3
This is not the end. THIS IS THE END  4
This is not the end. THIS IS THE END  5
This is not the end. THIS IS THE END  6
This is not the end. THIS IS THE END  7
This is not the end. THIS IS THE END  8
This is not the end. THIS IS THE END  9
This is not the end. THIS IS THE END  10

Error! - LINE INPUT truncation detected at PROGRAM line: 286 while reading file: D:\T\Test.txt
The actual truncated line of text:
This is not the end.

LINE INPUT statement (from keyboard)

Purpose:

Reads a line of text data from the keyboard. Similar to the INPUT statement, LINE INPUT allows text including commas and quotation marks to be input from the keyboard. See also the INPUT statement.

Syntax 1:

LINE INPUT Prompt AS STRING, Buffer AS STRING

Parameters:

  • Data type: STRING
    Prompt String variable or literal.
  • Data type: STRING
    Buffer String to receive the line input text.

Syntax 2:

LINE INPUT "", Buffer AS STRING

Parameters:

  • Data type: STRING
    "" Empty string for no prompt.
  • Data type: STRING
    Buffer String to receive the line input text.

Remarks:

LINE INPUT allows you to collect characters typed on the keyboard, including commas, without the special need to use quotation marks. This is mostly compatible with PowerBasic and Qbasic except that, in BCX, when LINE INPUT is used to collect characters typed on the keyboard, the prompt is not optional.

👉 It is important to note that the receiving string variable must be a statically declared string variable. It cannot be a dynamically declared string because BCX's current translation emits the compile-time C operator "sizeof" which does not work for determining the number of bytes that a dynamically declared string can hold.

Example 1:

Simple string variables

DIM a$
LINE INPUT " Name? ", a$
PRINT a$

Result:

Name? Noobie Suzy Coder
Noobie Suzy Coder

Example 2:

String Array variables

DIM b$[10,10]
LINE INPUT " Name? ", b$[5,5]
PRINT b$[5,5]

Result:

Name? Newby SiouxZe CodeR
Newby SiouxZe CodeR