Reputation: 99
So I am writing a program that will receive settings from a binary file in vb.net. I am reading 25 bytes at a time. However when I retrieve my byte array it is missing the first byte, and only the first byte.
Dim bytes(24) As Byte
Using fs As BinaryReader = New BinaryReader(File.Open(folder.SelectedPath & "\*********", FileMode.Open, FileAccess.Read))
While fs.Read() > 0
fs.Read(bytes, 0, bytes.Length)
End While
fs.Close()
End Using
My resulting array will miss only the first byte which in my case is 0x40. Why is this happening and what should I do to avoid this?
Upvotes: 1
Views: 118
Reputation: 27861
It is because the fs.Read
in While fs.Read() > 0
reads something from the stream, and thus the stream is no longer at position 0.
Here is how you should do it:
Dim bytes(24) As Byte
Using fs As BinaryReader = New BinaryReader(File.Open(folder.SelectedPath & "\*********", FileMode.Open, FileAccess.Read))
Dim total_read As Integer
While total_read < bytes.Length
Dim read = fs.Read(bytes, total_read, bytes.Length - total_read)
If read = 0 Then
Exit While
End If
total_read += read
End While
End Using
Upvotes: 3