468,505 Members | 1,592 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,505 developers. It's quick & easy.

Question with BinaryReader

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.
Nov 17 '05 #1
6 4091
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]
- mv*@spam.guard.caspershouse.com

"Question with BinaryReader" <Question with
Bi**********@discussions.microsoft.com> wrote in message
news:F3**********************************@microsof t.com...
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.

Nov 17 '05 #2
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..

Nicholas Paldino [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard.caspershouse.com

"Question with BinaryReader" <Question with
Bi**********@discussions.microsoft.com> wrote in message
news:F3**********************************@microsof t.com...
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.


Nov 17 '05 #3
Question with BinaryReader
<Qu**********************@discussions.microsoft.co m> wrote:
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 :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
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?
Nov 17 '05 #5
Question with BinaryReader
<Qu**********************@discussions.microsoft.co m> wrote:
My code as follows:
<snip>
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6
Thanks :)

"Jon Skeet [C# MVP]" wrote:
Question with BinaryReader
<Qu**********************@discussions.microsoft.co m> wrote:
My code as follows:


<snip>
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Bor de Wolf | last post: by
2 posts views Thread by I_AM_DON_AND_YOU? | last post: by
2 posts views Thread by Peter | last post: by
6 posts views Thread by Nikolay Petrov | last post: by
3 posts views Thread by =?Utf-8?B?VmljdG9y?= | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.