CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2004
    Posts
    80

    Skipping lines in readline

    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.

    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.

  2. #2
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

    Re: Skipping lines in readline

    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

  3. #3
    Join Date
    Sep 2004
    Posts
    80

    Re: Skipping lines in readline

    Thanks. Works like a charm.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured