Trouble w/ ReadInt32

  • Thread starter Michael_R_Banks
  • Start date
M

Michael_R_Banks

I'm getting erroneous data from subsequent calls to ReadInt32 -- I
have my FileStream and BinaryReader set up & accessing my binary
file. When I try to read the first 150 Int32's into an array using a
For-loop, the first read is a success (correct value), but then on the
next loop, the second call to ReadInt32 returns the same value as the
first read & the rest of the data is off also. Any ideas what I'm
doing wrong?

Loop: %%%%%%%%%%%%%%%%%%%%%%%
Dim Joints as New ArrayList
Dim j As Integer
Dim ARY As New MeasurementRecord
Dim tempvalue As Int32

For j = 0 To ((B_File.BaseStream.Length * 8) / 32) - 1
ARY.Clear()
For index = 0 To 149
tempvalue = B_File.ReadInt32
ARY.AddValue(index, tempvalue)
variablecount += 1
j += 1
Next index
Joints.Add(ARY)
Next

Class: %%%%%%%%%%%%%%%%%%%%%%%
Public Class MeasurementRecord
Dim classarray(149) As Int32

Public Sub AddValue(ByVal index As Integer, ByVal value As Int32)
classarray(index) = value
End Sub

Public Function Retreive(ByVal index As Integer) As Int32
Return classarray(index)
End Function

Public Sub Clear()
Dim index As Integer
For index = 0 To 149
classarray(index) = 0
Next
End Sub
End Class

Binary: %%%%%%%%%%%%%%%%%%%%%%%
0000 0001 0000 0000 0000 0000 0000 0000
0000 0004 0000 0029 0000 0000 0000 0000
0000 001D 0000 003D 0000 0000 0000 0000
 
M

Michael_R_Banks

For some reason, it is skipping the first three bytes before reading 4
bytes -- first read is 16777216 (0100 0000 hex), so my first statement
of the first read being correct was wrong.
 
A

Andrew Morton

Michael_R_Banks said:
I'm getting erroneous data from subsequent calls to ReadInt32 -- I
have my FileStream and BinaryReader set up & accessing my binary
file.

But you haven't shown that bit of code.
When I try to read the first 150 Int32's into an array using a
For-loop, the first read is a success (correct value), but then on the
next loop, the second call to ReadInt32 returns the same value as the
first read & the rest of the data is off also. Any ideas what I'm
doing wrong?

If we could see the code...
Loop: %%%%%%%%%%%%%%%%%%%%%%%
Dim Joints as New ArrayList
Dim j As Integer
Dim ARY As New MeasurementRecord
Dim tempvalue As Int32

For j = 0 To ((B_File.BaseStream.Length * 8) / 32) - 1

If you use "For j As Integer = 0 To ((B_File.BaseStream.Length * 8) / 32) -
1" then the scope of j is limited to the loop and you can't accidentally use
it outside the loop.
ARY.Clear()
For index = 0 To 149
tempvalue = B_File.ReadInt32
ARY.AddValue(index, tempvalue)
variablecount += 1
j += 1

Why are you interfering with the loop index? You /can/, but why not use "For
j = 0 To ((B_File.BaseStream.Length * 8) / 32) - 1 Step 2"?
Next index
Joints.Add(ARY)
Next

I have a suspicion you've used j to decide where to start the next batch of
reading.

I know, that's more questions than answers.

Andrew
 
M

Michael_R_Banks

Thanks for your input! I changed my code to B_File.ReadBytes(4)
instead of ReadInt32 and it works fine now. I'll have to go back and
check my implementation of the loop index (j) to see if there was
anything strange there, especially since I'm still missing some data.

Regards,
Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top