LineInput Function
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

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



End of file reached.



FileNumber does not exist.

See the "Error number" column if you are upgrading Visual Basic 6.0 applications that use unstructured error handling. (You can compare the error number against the Number Property (Err Object).) However, when possible, you should consider replacing such error control with Structured Exception Handling Overview for Visual Basic.

The LineInput function is provided for backward compatibility and may have an impact on performance. For non-legacy applications, the My.Computer.FileSystem object provides better performance. For more information, see File Access with Visual Basic.

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

Security noteSecurity Note

When reading from files, do not make decisions about the contents of a file based on the file name extension. For example, a file named Form1.vb may not be a Visual Basic source file.

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

Security noteSecurity Note

Reading from a file with the LineInput function requires Read access from the FileIOPermissionAccess enumeration.

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
' Open file.
FileOpen(1, "TESTFILE", OpenMode.Input)
' Loop until end of file.
While Not EOF(1)
   ' Read line into variable.
   TextLine = LineInput(1)
   ' Print to the console.
   WriteLine(1, TextLine)
End While

This function is not supported.

Namespace: Microsoft.VisualBasic

Module: FileSystem

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

© 2015 Microsoft