Problem using StreamReader and BinanryReader / Writer Together.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am rewriting a C++ application in C#. This file has a combination of Text
and Binary data.

I used CFile before to read the text. If I hit a certain string that
denotes the following data is binary, I used the current position in the
file and another stream to read to the binary data.

All text data is ended with a carriage return / line feed while the binary
is actually an image file listed byte by byte. Preceding the binary data is
a text value ended by a cr/lf listing the actual number of bytes of binary
data.

The problem is that the position given by streamreader.basestream seems to
be
actually twice the actual position in bytes. Therefore positioning a Binary
Reader based on the position in a Text Reader is incorrect.

This worked with CFile.

Any ideas?

Thank You,
Jeff
 
Most likely the issue is that in .NET, every string is a
unicode string by default. Thus every character is 2
bytes.

You can probably get around this by making sure your
stream reader sets its encoding to ASCII when it opens
the file (Encoding.ASCII).

-----Original Message-----
I am rewriting a C++ application in C#. This file has a combination of Text
and Binary data.

I used CFile before to read the text. If I hit a certain string that
denotes the following data is binary, I used the current position in the
file and another stream to read to the binary data.

All text data is ended with a carriage return / line feed while the binary
is actually an image file listed byte by byte. Preceding the binary data is
a text value ended by a cr/lf listing the actual number of bytes of binary
data.

The problem is that the position given by
streamreader.basestream seems to
 
Phil Haack (haacked.com) said:
Most likely the issue is that in .NET, every string is a
unicode string by default. Thus every character is 2
bytes.

You can probably get around this by making sure your
stream reader sets its encoding to ASCII when it opens
the file (Encoding.ASCII).

No, that's not the issue. The issue is the buffering that the
StreamReader performs - it reads from the stream past where you read
characters from it, so that it can be more efficient.
 
Back
Top