Line Input# Statement

Syntax

Line Input #filenumber, varname

filenumber

Use: Required

Data Type: Integer

Any valid file number.

varname

Use: Required

Data Type: String or Variant

The name of a string or variant variable.

Description

Assigns a single line from a sequential file opened with the For Input method to a string or variant variable.

Rules at a Glance

  • Data is read into a buffer one character at a time until a line feed or carriage return sequence (either Chr(13) or Chr(13)+Chr(10)) is encountered. When this happens, all characters in the buffer are assigned to varname as a single string, without the carriage return sequence, and the buffer is cleared.

  • After reading a line, the file pointer advances to the first character after the end of the line or to the end-of-file marker.

Example

Dim intLine As Integer, hFile As Integer
Dim strBuffer As String

hFile = FreeFile
Open "lineinp.txt" For Input As #hFile
Do While Not EOF(hFile)
   intLine = intLine + 1
   Line Input #hFile, strBuffer
   List1.AddItem strBuffer
Loop

Close #hFile

Programming Tips and Gotchas

You use the Line Input statement to read data from unstructured sequential data files. To write data back to this type of file, use the Print # statement.

Get VB & VBA in a Nutshell: The Language now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.