Reading a file error in VB.NET?
The way this file works, there is an empty buffer, then the checksum is the user then the byte, which gives the user the count of the name of the letter, then the byte for how many bytes to go to the next user and the byte for which the user file, the user saves their settings.
the usersm loop in the IF statement sets the entire file stream to be retrieved. However, almost exactly the same code Disclaimers still specifically str.Read (xnl, 0, USN - 1) in the code still seems that reading the very beginning of the file, despite the position of FileStream being set earlier, does anyone know What is happening here?
it's 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
a source to share
When you change the position of the base stream, it StreamReader
doesn't know that you did it. If the previously read "too much" data (intentionally, for the sake of efficiency - it tries to avoid doing a lot of little reads on the main stream), then it will buffer the data that it will use instead of talking directly to the attached stream. You need to call StreamReader.DiscardBufferedData
after repositioning the flow to avoid this.
a source to share