Visual Basic Language Reference  

LineInput Function

Reads a single line from an open sequential file and assigns it to a String variable.

Public Function LineInput(ByVal FileNumber As Integer) As String


Required. Any valid file number.


Exception type Error number Condition
EndOfStreamException 62 End of file reached.
IOException 52 FileNumber does not exist.


Data read with LineInput is usually written to a file with Print.

The LineInput function reads from a file one character at a time until it encounters a carriage return (Chr(13)) or carriage return–linefeed (Chr(13) + Chr(10)) sequence. Carriage return–linefeed sequences are skipped rather than appended to the character string.


This example uses the LineInput function to read a line from a sequential file and assign it to a variable. This example assumes that TESTFILE is a text file with a few lines of sample data.

Dim TextLine As String
FileOpen(1, "TESTFILE", OpenMode.Input)   ' Open file.
While Not EOF(1)   ' Loop until end of file.
   TextLine = LineInput(1)   ' Read line into variable.
   Debug.WriteLine(TextLine)   ' Print to the console.
End While
FileClose(1)   ' Close file.

See Also

Chr, ChrW Functions | Input Function | EndOfStreamException | IOException