I would do this with perl's builtin functions, not File::Binary.

To search for a string in a binary file, you can use a sliding window, such as

my $s = "YOUR_NEEDLE"; open my $f, "<", "YOUR_FILENAME" or die "open error: $!"; binmode $f o +r die "binmode error: $!"; my($b0, $b1) = ""; my($r, $g) = 0; do { $b1 = $b0; $r = read($f, $b0, 1024); defined($r) or die "read err +or: $!"; $r == 0 and die "string not found"; } until 0 <= ($g = index($b1 . $b0, $s)); my $pos = tell($f) - length($b0) - length($b1) + $g + length($s);

(Update: the easy way is { local $/ = $s; defined(readline $f) or die "error read: $!"; } $pos = tell($f);, but that reads a large chunk of file to memory.)

Then you can seek to $pos, and read the data from there with read. You can interpret binary data with unpack.

Update: corrected error in first sentence, noticed by BrowserUk.


In reply to Re: File::Binary help by ambrus
in thread File::Binary help by PrimeLord

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.