Reading a Text File Line by Line

Ciduletz1983

Active member
Joined
Oct 10, 2006
Messages
30
Location
Constanta, Romania
Programming Experience
Beginner
Good day. I learn VB.NET alone. No support. Just some dumb tutorials. I can`t understand the meaning of "peek" and " -1 " in this code:

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim FILE_NAME As String = "C:\test.txt"
        Dim TextLine As String

        If System.IO.File.Exists(FILE_NAME) = True Then

            Dim objReader As New System.IO.StreamReader(FILE_NAME)

            Do While objReader.Peek() <> -1
                TextLine = TextLine & objReader.ReadLine() & vbNewLine
            Loop

            TextBox1.Text = TextLine

        Else

            MsgBox("File Does Not Exist")

        End If
    End Sub

Could someone here explain me ? Please!
 
Hi there,

The peek in this code is essentially telling us when there is nothing left to read in. Peek will return the next character along without moving the streamreader object along. If peek returns -1 it means there is nothing left to read after the current position therefore in this example, we know it is the end of the file. Once the end of the file is reaached it breaks out of the loop and displays the text in a text box. For more on Peek take a look here:

http://msdn2.microsoft.com/en-us/library/system.io.streamreader.peek.aspx

Cheers,
Jamie.
 
If Peek finds a next charactor, what ever it may be, it returns a value of 1 (or some number, I think its 1). If Peek tries to find the next charactor but instead encounters the EOF (End of File) it returns a -1.

The code is asking Peek what the next charator is, if its anything besides -1, do the loop again, if -1 (EOF) then exit the loop and continue the program.
 
Raven65 Thx for your time. i added u to my friends list :) Do you have an ym id or msn id ?:) Jamie thx 2 you 2 .. you are on my friends list now 2 :):)
 
Last edited by a moderator:
No problem at all, glad between the two of us you got a better understanding.

Be sure to post your future questions here in the forums though rather than private messages so that future visitors might find the answers when they have the same questions!
 
additionally, posting your email/messenger sign in address on the web will increase the amount of spam you receive. your call! :)
 
In the code above i get the whole contents of the c:\test.txt file in a TextBox. Why the text doesn`t appear line by line in the textbox ?

Edit:
Hm i think i know.. it`s this line
VB.NET:
TextLine = TextLine & objReader.ReadLine() & vbNewLine

But i don`t really understand the vbNewLine here..And another thing.. what if i wanted that the content of the c:\test.txt file be displayed line by line.. I mean .. a line at a time..and only that line being visible.. the rest not.
 
Last edited:
vbNewLine represents an "Enter", if it wasn't there, all lines of the file would display in the same line in your textbox1

about displaying line by line, i don't really know what you mean, try this:

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
   Dim FILE_NAME As String = "C:\test.txt"
   Dim TextLine As String
 
    If System.IO.File.Exists(FILE_NAME) = True Then
 
        Dim objReader As New System.IO.StreamReader(FILE_NAME)
 
        Do While objReader.Peek() <> -1
           TextBox1.Text = objReader.ReadLine() 
           'TextLine = TextLine & objReader.ReadLine() & vbNewLine
           System.Threading.Thread.Sleep(1000)
           [SIZE=2]Application.DoEvents()[/SIZE]
        Loop
 
    Else
 
        MsgBox("File Does Not Exist")
 
    End If
End Sub

this will display every single line for 1 second in textbox1
Sleep(1000) in the loop will make the CPU halt 1 second after reading each line
 
Last edited:
The code you wrote is not good. It displays only one line of text. I want every time i hit the button to display one line of text in order until there`s nothing to display.. you understand ?:)
 
you are right, i edited my code in the previous post, now it should display line per line in the textbox1

Application.DoEvents makes the textbox1 refresh.

But it's not quite what you want apparently, you want to jump to a specific line in the file when the button is pressed. I'll try to to that.
 
I found exactly what you wanted. I added a second button to the form

try this:

VB.NET:
Private objReader As System.IO.StreamReader
 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim FILE_NAME As String = "C:\test2.txt"
    Dim TextLine As String
       If System.IO.File.Exists(FILE_NAME) = True Then
          objReader = New System.IO.StreamReader(FILE_NAME)
       Else
          MsgBox("File Does Not Exist")
       End If
End Sub
 
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
     If objReader.Peek() <> -1 Then
         TextBox1.Text = objReader.ReadLine()
     End If
End Sub

Button1 will set/reset the streamreader (position in the file = line 0)
Button2 will read the next line in the same streamreader and display it in textbox1 (that's why streamreader is declared outside button1_Click and Button2_Click, so you can use it anywhere in your form)
 
oo waiiit a minute.. i get an error if i hit Button_2 first.. i`ll try to fix this.. Hope i`ll succed :)


Edit:
Cygnus this code is good only if i hit the button 1 first .. but i want to fix the problem hitting the second button first. If i hit the second button first VB doesn`t know WHAT to read..becouse you told him only what your are using to read the file. The declaration above the two buttons is not enough.
 
Last edited:
Back
Top