Jim
Jim

Reputation: 601

Reading a file bug in VB.NET?

The way this file works is there is a null buffer, then a user check sum then a byte that gives you the user name letter count, then a byte for how many bytes to skip to the next user and a byte for which user file the user keeps their settings in.

the loop with the usersm variable in the IF statement sets up the whole file stream for extraction. However with almost the exact same code the else clause specifically the str.Read(xnl, 0, usn - 1) in the else code appears to be reading the very beginning of the file despite the position of the filestream being set earlier, anyone know whats happening here?

this is in vb2005

Private Sub readusersdata(ByVal userdatafile As String)
    ListView1.BeginUpdate()
    ListView1.Items.Clear()
    Using snxl As IO.Stream = IO.File.Open(userdatafile, IO.FileMode.Open)

        Using str As New IO.StreamReader(snxl)
            str.BaseStream.Position = 4
            Dim usersm As Integer = str.BaseStream.ReadByte()
            Dim users As Integer = usersm
            While users > 0
                If usersm = users Then
                    Dim trailtouser As Integer = 0
                    str.BaseStream.Position = 6
                    Dim ust As Integer = str.BaseStream.ReadByte()
                    str.BaseStream.Position = 8
                    Dim snb(ust - 1) As Char
                    str.ReadBlock(snb, 0, ust)
                    Dim bst = New String(snb)
                    If usersm = 1 Then
                        str.BaseStream.Position = 16
                    Else
                        str.BaseStream.Position = 15
                    End If
                    cLVN(ListView1, bst, str.BaseStream.ReadByte)
                    str.BaseStream.Position = 8 + snb.Length
                    str.BaseStream.Position += str.BaseStream.ReadByte + 1
                Else

                    Dim usn As Integer = str.BaseStream.ReadByte
                    str.BaseStream.Position += 2
                    Dim chrpos As Integer = str.BaseStream.Position
                    Dim xnl(usn - 1) As Char
                    str.Read(xnl, 0, usn - 1)
                    Dim skpbyte As Integer = str.BaseStream.ReadByte
                    str.BaseStream.Position += 3
                    Dim udata As Integer = str.BaseStream.ReadByte


                End If

                users -= 1

            End While
        End Using
    End Using
    ListView1.EndUpdate()
End Sub

Upvotes: 0

Views: 186

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500055

When you change the position of the underlying stream, the StreamReader doesn't know you've done that. If it's previously read "too much" data (deliberately, for the sake of efficiency - it tries to avoid doing lots of little reads on the underlying stream) then it will have buffered data that it'll use instead of talking directly to the repositioned stream. You need to call StreamReader.DiscardBufferedData after repositioning the stream to avoid that.

Upvotes: 2

Related Questions