Reading files in Visual Basic, GW-Basic style

Recently I was asked how to read a file without using streams in Visual Basic.

Of course, if you’ve done this once in GW-Basic, you never forget.


    Dim iFileNumber As Integer
    Dim sLine As String

    iFileNumber = FreeFile
    Open "C:\stats.log" For Input As iFileNumber
    Do While Not EOF(iFileNumber)
        Input #iFileNumber, sLine
        MsgBox sLine
    Loop

Join the Conversation

2 Comments

  1. If you are wanting to read a whole line (up to the next cr/lf pair), ammend your code to:

    Line Input #iFileNumber, sLine

    Input by itself is used to read comma seperated values in the order they were written:

    Print #iFileNumber, 1, “A test”

    Input #iFileNumber, iMyInt, sMyString

    Hope this helps :-)

Leave a comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.