BinaryReader munges data

K

Kevin Trojanowski

I'm about to rip out what little hair I have left; I have a class that
uses a BinaryReader, and the data is getting munged. A subset of the
code follows; I've removed the parts that aren't relevant.

What's really strange is that it reads the file just fine until the
272nd call to the Read method of the class, at which point it begins
munging the data.

Any thoughts would be greatly appreciated; I've tried reading by byte,
as well as reading blocks, and it makes no difference.

-Kevin

public class HeaderStream
{
private const int c_BlockSize = 680;

private System.IO.BinaryReader str;

public HeaderStream(string path)
{
FileStream s = new FileStream(path,
FileMode.Open,
Read);

str = new BinaryReader(s);
}

public OWHeader Read()
{
byte[] buf = new byte[c_BlockSize];
int i;

i = str.Read(buf, 0, c_BlockSize);
if (i != c_BlockSize)
throw new EndOfStreamException(
String.Format(
"End of stream reached with bytes left to read"));

return new OWHeader(buf);
}
}
 
D

Daniel O'Connell [C# MVP]

Kevin Trojanowski said:
I'm about to rip out what little hair I have left; I have a class that
uses a BinaryReader, and the data is getting munged. A subset of the code
follows; I've removed the parts that aren't relevant.

What's really strange is that it reads the file just fine until the 272nd
call to the Read method of the class, at which point it begins munging the
data.

Any thoughts would be greatly appreciated; I've tried reading by byte, as
well as reading blocks, and it makes no difference.

How is it munging it? And could you show us a short but complete program[1]
that exhibits your problem? The code below just doesn't show whats going to
happen.

1. http://www.yoda.arachsys.com/csharp/complete.html
 
K

Kevin Trojanowski

Daniel said:
How is it munging it? And could you show us a short but complete program[1]
that exhibits your problem? The code below just doesn't show whats going to
happen.

Nevermind; I found the problem -- I was looking at the wrong record (ah,
the joys of high data offsets). Records 272 and 273 look for very
similar, and I was comparing 272 in the file against 273 in the buffer.

Don't I feel stupid....

-Kevin
 
D

Daniel O'Connell [C# MVP]

Kevin Trojanowski said:
Daniel said:
How is it munging it? And could you show us a short but complete
program[1] that exhibits your problem? The code below just doesn't show
whats going to happen.

Nevermind; I found the problem -- I was looking at the wrong record (ah,
the joys of high data offsets). Records 272 and 273 look for very
similar, and I was comparing 272 in the file against 273 in the buffer.

Don't I feel stupid....

It happens, ;).
 

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