Line Input # 语句

从顺序文件将字符串并读取到变量。

语法:

Line Input #FileNumber As Integer, Var As String

参数:

FileNumber:要从中读取数据的文件编号。该文件必须先用含有 INPUT 关键字的 Open 语句打开。

Var:用于存储结果的变量名称。

使用 Line Input# 语句,可以从打开的文件中读取字符串并将它写入一个变量。逐行读取字符串变量的过程在遇到第一个回车 (Asc=13) 或换行 (Asc=10) 时停止。得到的字符串中不包括行尾标记。

示例:

Sub ExampleWorkWithAFile

Dim iNumber As Integer

Dim sLine As String

Dim aFile As String

Dim sMsg As String

    aFile = "c:\data.txt"

    iNumber = Freefile

    Open aFile For Output As #iNumber

    Print #iNumber, "This is a line of text"

    Print #iNumber, "This is another line of text"

    Close #iNumber

    iNumber = Freefile

    Open aFile For Input As iNumber

    While Not EOF(iNumber)

        Line Input #iNumber, sLine

        If sLine <>"" Then

            sMsg = sMsg & sLine & chr(13)

        End If

    Wend

    Close #iNumber

    MsgBox sMsg

End Sub