Question with BinaryReader

G

Guest

I use BinaryReader to read my binary dafa files, when i call ReadBytes, why
it always return more 4 bytes. The following is my code.

FileStream fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes(8);
bytes = br.ReadBytes(1);

After the first sentence, fs.Position is 0, and all things is ok, but when
runs to the second sentence, i found fs.Position is 4, when runs to the third
sentence, i got the wrong data, and fs.Position is 16, after the fourth
sentence, fs.Position is 21.
I try to reset fs.Position to 0 before the construct br, but failed.

Thanks.
 
N

Nicholas Paldino [.NET/C# MVP]

I believe the BinaryReader caches the reads from the underlying stream
(I can't remember). However, this shouldn't be of concern to you. Rather,
can you provide a file as well as the code which shows the error? Looking
at this here, I would expect you would get the first 8 bytes, then the 9th
byte, and I can't see the problem (unless I see the file and what you
expect).

Also, if you are simply reading bytes or arrays of bytes, you can just
use the FileStream, there is no need for the BinaryReader.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

"Question with BinaryReader" <Question with
(e-mail address removed)> wrote in message
news:[email protected]...
 
G

Guest

The fact is I can't get the expedted data. I'll tell the details with
First 8 bytes of my data file is: "SBMP2.0"(end with \0), then I use
Byte[] bytes = r.ReadBytes(8);
to read file type("SBMP2.0") to bytes array, but the result is
bytes[0] is 50 '2'
bytes[1] is 46; '.'
bytes[2] is 48; '0'
bytes[3] is 0;
bytes[4] is 188
bytes[5] is 1;
bytes[6] is 0;
bytes[7] is 0;

I look up fs._buffer througth monitor window is:
fs._buffer[0] is 83 'S'
fs._buffer[1] is 66; 'B'
fs._buffer[2] is 77; 'M'
fs._buffer[3] is 80; 'P'
fs._buffer[4] is 50 '2'
fs._buffer[5] is 46; '.'
fs._buffer[6] is 48; '0'
fs._buffer[7] is 0; '\0'

where is "SBMP" going?

I use BinaryReader because I want to use other methods of it, such as
ReadInt16..
I believe the BinaryReader caches the reads from the underlying stream
(I can't remember). However, this shouldn't be of concern to you. Rather,
can you provide a file as well as the code which shows the error? Looking
at this here, I would expect you would get the first 8 bytes, then the 9th
byte, and I can't see the problem (unless I see the file and what you
expect).

Also, if you are simply reading bytes or arrays of bytes, you can just
use the FileStream, there is no need for the BinaryReader.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

"Question with BinaryReader" <Question with
(e-mail address removed)> wrote in message
I use BinaryReader to read my binary dafa files, when i call ReadBytes, why
it always return more 4 bytes. The following is my code.

FileStream fs = new FileStream(file, FileMode.OpenOrCreate,
FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes(8);
bytes = br.ReadBytes(1);

After the first sentence, fs.Position is 0, and all things is ok, but when
runs to the second sentence, i found fs.Position is 4, when runs to the
third
sentence, i got the wrong data, and fs.Position is 16, after the fourth
sentence, fs.Position is 21.
I try to reset fs.Position to 0 before the construct br, but failed.

Thanks.
 
J

Jon Skeet [C# MVP]

Question with BinaryReader
The fact is I can't get the expedted data. I'll tell the details with
First 8 bytes of my data file is: "SBMP2.0"(end with \0), then I use
Byte[] bytes = r.ReadBytes(8);
to read file type("SBMP2.0") to bytes array, but the result is
bytes[0] is 50 '2'
bytes[1] is 46; '.'
bytes[2] is 48; '0'
bytes[3] is 0;
bytes[4] is 188
bytes[5] is 1;
bytes[6] is 0;
bytes[7] is 0;

I look up fs._buffer througth monitor window is:
fs._buffer[0] is 83 'S'
fs._buffer[1] is 66; 'B'
fs._buffer[2] is 77; 'M'
fs._buffer[3] is 80; 'P'
fs._buffer[4] is 50 '2'
fs._buffer[5] is 46; '.'
fs._buffer[6] is 48; '0'
fs._buffer[7] is 0; '\0'

where is "SBMP" going?

I use BinaryReader because I want to use other methods of it, such as
ReadInt16..

It's hard to say without a full example. Could you post a short but
complete program which demonstrates the problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

There's bound to be something simple - BinaryReader *does* work,
honestly :)
 
G

Guest

My code as follows:

using System;
using System.IO;

namespace Test
{
public class Test
{
public Test()
{
}
static void Main()
{
String file = @"C:\page.sb";
FileStream fs = new FileStream(file, FileMode.Open,
FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes(7);
String temp = System.Text.Encoding.UTF8.GetString(bytes);
if ( br.ReadChar() != '\0' || temp != "SBMP2.0" )
{
br.Close();
fs.Close();
}
Int32 offset = br.ReadInt32();
Int32 length = br.ReadInt32();
br.Close();
fs.Close();
}
}
}

So strangely, my code works well doday. But yesterday, it did the wrong
thing. I haven tried several times, the result is same.

I post my code here, please help me explain why happend that?
 
J

Jon Skeet [C# MVP]

Question with BinaryReader
My code as follows:

So strangely, my code works well doday. But yesterday, it did the wrong
thing. I haven tried several times, the result is same.

I post my code here, please help me explain why happend that?

I suspect you weren't running the code you thought you were running -
possibly the executable was still in use when you tried to compile it?
I can't see why that code would fail in the way you explained.
 
G

Guest

Thanks :)

Jon Skeet said:
Question with BinaryReader




I suspect you weren't running the code you thought you were running -
possibly the executable was still in use when you tried to compile it?
I can't see why that code would fail in the way you explained.
 

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