O'Reilly logo

VB.NET Language in a Nutshell, Second Edition by Steven Roman PhD, Paul Lomax, Ron Petrusha

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

LineInput Function

Class

Microsoft.VisualBasic.FileSystem

Syntax

LineInput(filenumber)
filenumber (required; Integer)

Any valid file number

Return Value

A String containing the line read from the file

Description

Assigns a single line from a sequential file opened in Input mode to a string 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 the characters in the buffer are returned as a 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

The following code reads all of the lines in a text file and sends them to the Output window:

Dim fr As Integer = FreeFile(  )
Dim sLine As String
FileOpen(fr, "c:\data.txt", OpenMode.Input, OpenAccess.Read)
Do While Not EOF(fr)
    Console.WriteLine(LineInput(fr))
Loop
FileClose(fr)

Programming Tips and Gotchas

You use the LineInput function to read data from text files. To write data back to this type of file, use the PrintLine function.

VB.NET/VB 6 Differences

The VB.NET LineInput function corresponds directly to the VB 6 LineInput statement, with the following differences:

  • The VB 6 LineInput statement has a second argument, varname, which is the variable to receive the line read by the function. It is not supported by the VB.NET LineInput function, since the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required