views:

80

answers:

1

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
+1  A: 

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.

Jon Skeet
Its asinine that if you manipulate the stream under the streamreader that it doesnt keep up with the changes, this works thank you
Jim
Why is it asinine? How would the StreamReader know? For 99% of cases, you *don't* mess with the underlying stream and buffering is very helpful. Also remember that not all streams even support the notion of seeking or finding the current position, which would make it even harder to detect.
Jon Skeet
you would think that if the internal pointer changed on a stream that the streamreader was using as a resource that it would update its own pointer. That was something i didnt think i had to worry about. In this program i have to jump around a little bit to get the data i need both in bytes and in strings
Jim
But how would the StreamReader know that the underlying stream had been repositioned? There's no event to indicate seeking.
Jon Skeet
the stream reader should be using the same pointer as the internal stream therefore no event should be necessary, im going to look and see if i can disable buffering for this.
Jim
So you're saying you believe StreamReader should never use any internal buffer? Those of us who *aren't* in your situation wouldn't thank you for that. Reading a mixture of text and binary data is relatively rare - the normal case is *just* reading text, at which point using a buffer is entirely natural. Why tailor the code to the rare case?
Jon Skeet