Click to See Complete Forum and Search --> : Skipping lines in readline


doomsday123
October 8th, 2004, 01:35 PM
Im trying to read each line of a file and output it to a file. I am splitting each line at the "," so i can display them in colums in the listbox. When I click the display button it goes in and readlines the file until readline = "" but for some reason it is skipping every other line. Here is my code.


Private Sub Display()
Dim sr1 As IO.StreamReader = New IO.StreamReader("File.txt")
Dim Data As String
Dim strfrm As String = "{0,0} {1,30}"
Dim count As Integer = 0

Do Until sr1.ReadLine = ""
Data = sr1.ReadLine
Dim Items() As String = Data.Split(",")
lstDisplay.Items.Add(Items(0).PadRight(25, " ") & Items(1))
Loop

sr1.Close()
End Sub

Any help would be great. Thanks.

DSJ
October 8th, 2004, 01:44 PM
That's because your reading twice in each loop:

Do Until sr1.ReadLine = "" '<---- ONCE HERE
Data = sr1.ReadLine '<---- AND ONCE HERE
Dim Items() As String = Data.Split(",")
lstDisplay.Items.Add(Items(0).PadRight(25, " ") & Items(1))
Loop

Try something more along these lines (you can take out the progress bar code if you don't want/need it):

Dim sr As StreamReader
Dim Pos As Long
Dim Length As Long
Dim Rec As String
Dim Cnt As Long

Try
sr = New StreamReader(tbFilename.Text)
pbProgress.Visible = True
pbProgress.Value = 0
Length = sr.BaseStream.Length
Do While sr.Peek <> -1
Rec = sr.ReadLine
'DO YOUR STUFF HERE
Cnt += 1
pbProgress.Value = CInt(sr.BaseStream.Position / Length * 100)
Loop
Catch ex As Exception
MsgBox(ex.Message)
End Try

doomsday123
October 8th, 2004, 02:01 PM
Thanks. Works like a charm.