Re: Search through a (large) binary file.
- From: "Peter Duniho" <no.peted.spam@xxxxxxxxxxxxxxxxxx>
- Date: Mon, 14 Sep 2009 10:05:15 -0700
On Mon, 14 Sep 2009 05:40:08 -0700, Michelle <michelle@xxxxxxxxxxxxxxx> wrote:
[...]
I'm not happy about this; FileStream fs = File.OpenRead(@"D:\myfile.bin");
Because it opens the file for reading the second time.
It works, but I'm not sure this is the best solution.
Please give me feedback.
The only reason to use BinaryReader is if you want to convert directly from bytes read to some more complex type. If you're only ever going to read the plain bytes, just read them directly from the FileStream you're working with. You can use the Position property to adjust from where you're reading in the file; save the current position, set the current position to 4 bytes earlier than the offset of the found string, read the 4 bytes of interest, then restore the current position to the previously saved value.
Your processing of those four bytes seems odd too, but perhaps we just don't know enough about the format. If you have 4 bytes in an array, it would seem that you ought to just be using BitConverter to convert those directly to a 32-bit int, rather than doing all that stuff with the string.
If, as it appears from your conversion code, the bytes in the file are big-endian (most-significant-byte first) you can handle that much more cleanly simply by swapping the bytes before converting to a 32-bit int. If you like, you can just encapsulate that in a single method:
int Int32FromMSBArray(byte[] rgb)
{
byte bT = rgb[0];
rgb[0] = rgb[3];
rgb[3] = bT;
bT = rgb[1];
rgb[1] = rgb[2];
rgb[2] = rgbT;
return BitConverter.ToInt32(rgb, 0);
}
(The above code modifies the input array; normally this shouldn't be a problem, but if you need the original array preserved, you'll want to make a copy).
Pete
.
- Follow-Ups:
- Re: Search through a (large) binary file.
- From: Michelle
- Re: Search through a (large) binary file.
- From: Michelle
- Re: Search through a (large) binary file.
- From: Peter Duniho
- Re: Search through a (large) binary file.
- References:
- Search through a (large) binary file.
- From: Michelle
- Re: Search through a (large) binary file.
- From: Michelle
- Re: Search through a (large) binary file.
- From: Peter Duniho
- Re: Search through a (large) binary file.
- From: Michelle
- Re: Search through a (large) binary file.
- From: Peter Duniho
- Re: Search through a (large) binary file.
- From: Michelle
- Search through a (large) binary file.
- Prev by Date: Re: thread safe access to string[] without lock
- Next by Date: Re: Search through a (large) binary file.
- Previous by thread: Re: Search through a (large) binary file.
- Next by thread: Re: Search through a (large) binary file.
- Index(es):
Relevant Pages
|