Re: Search through a (large) binary file.

Tech-Archive recommends: Fix windows errors by optimizing your registry



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
.



Relevant Pages

  • Re: [perfmon] Re: [perfmon2] perfmon2 merge news
    ... where the caller supplies an array of PMD numbers and the function ... returns their values (and you want that reading to be done atomically ... int *pmd_numbers; ...
    (Linux-Kernel)
  • [MC++] __nogc arrays of __value classes
    ... I have been reading through the ManagedExtensionsSpec.doc file and I ... make a difference) and I want a non-variable-size array of them, ... __value struct Contours { ... int num_contours; ...
    (microsoft.public.dotnet.languages.vc)
  • Re: [C] functions and 2D arrays?
    ... >to arrays of arrays of int, when you could be passing pointers ... >to arrays of int. ... to array" is rather out of the ordinary. ... feels more "natural" after you've been reading and writing C and C++ ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Elegant way to do this?
    ... > press enter to retrieve next array value. ... > to type 'int' can equal the given constant ... > Is there an elegant solution to reading values from arrays or reading ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Really Stuck, Please Help
    ... I am not sure how to construct an array. ... Reading Master Log", of the "Copreco Master Log" workbook. ... Dim sourceBook As String ...
    (microsoft.public.excel.programming)