Get Line From File Method

The Get Line From File method reads a line from a sequential file, and then saves it in a string variable. It does not return a value. You can use it to read lines of text from a text file where a carriage return separates each data element. You can use a read method to read data from a file that includes values and commas that separate each of these values.

Format A

Line Input [#] filenumber, varName
Line Input [prompt,] varName

The following table describes the arguments that you can use with this method.

Argument Description

filenumber

This method does the following depending on if you include the filenumber argument:

  • Include filenumber argument. It uses the file number that the Open statement uses to open the file.

  • Do not include filenumber argument. It reads the line from the keyboard.

varName

A string variable. This method saves a line of data or user input into this variable.

prompt

A string literal that prompts the user for keyboard input. If you do not include the prompt argument, then this method displays a question mark (?) as the prompt.

Example

The following example reads the contents of a sequential file line by line up to a carriage return, and then displays the result. The CreateFile subroutine creates the C:\temp001 file that the main subroutine uses:

(general) (declarations)
Option Explicit
Declare Sub CreateFile
Sub CreateFile
   Rem Put the numbers 1-10 into a file
   Dim x as Integer
   Open "c:\temp001" for Output as #1
   For x = 1 to 10
      Write #1, x
   Next x
   Close #1
End Sub
Sub Button_Click
   Dim testscore as String
   Dim x
   Dim y
   Dim newline
   Call CreateFile
   Open "c:\temp001" for Input as #1
   x = 1
   newline = Chr(10)
   msgtext = "The contents of c:\temp001 is: " & newline
   Do Until x = Lof(1)
      Line Input #1, testscore
      x = x + 1
      y = Seek(1)
      If y>Lof(1) then
         x = Lof(1)
      Else
         Seek 1,y
      End If
      msgtext = msgtext & testscore & newline
   Loop
   Close #1
      Kill "c:\temp001"
End Sub