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. 6 4302
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.
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.
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
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?
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Chris P. |
last post by:
I have a C# application that connects to Perl application on a UNIX server.
I am able to connect and communicate both directions with the server, but
there are several occasions when it appears...
|
by: Denny Rue |
last post by:
I’ve created VB code to download files from a web site through the use of
HTTPWebRequest, HTTPWebResponse and BinaryReader.
The HTTPWebRequest has a TimeOut property to limit how long it waits...
|
by: Bor de Wolf |
last post by:
Please tell me why I am not getting out what I put in.
FileStream nrIn = new
FileStream("DataCount.prl",FileMode.OpenOrCreate,FileAccess.Read) ;
BinaryReader inr = new BinaryReader(nrIn);...
|
by: Laszlo Szijarto |
last post by:
Can BinaryReader be forced to read a stream, say a TCP/IP stream or memory
stream or even file stream in big endian order or do I have to write
something custom to reverse the byte order? So, for...
|
by: Robert Misiak |
last post by:
I've been trying out the .NET Memory Profiler
(http://www.scitech.se/memprofiler/) and I found an excessive number of
undisposed instances of BinaryReader after opening and then closing various...
|
by: I_AM_DON_AND_YOU? |
last post by:
Can BinaryReader Object read the Word file?
|
by: Peter |
last post by:
I have a component that encrypts/decrypts files using Rijndael
encryption. The component works flawlessly but I want to try to
extend its capablities and this is my general question:
I have an...
|
by: Nikolay Petrov |
last post by:
I need to transfer different types of data from my tcp client to my tcp
server - string, bynary, datasets.
Ho to know what exactly data I receive?
|
by: =?Utf-8?B?VmljdG9y?= |
last post by:
Hi,
Could you tell me can I keep the MemoryStream open and "close" the
BinaryReader?
As the MemoryStream is used for buffering the TCP data and BinaryReader is
only used to read the...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |