Or you could depend on the buffering power of the IO libraries and inspect your data four-bytes at a time, rewinding three bytes if no match is found. Not as fast as fixed size records but not as bad as your probably think it is.
#!/usr/bin/perl -wd
$file = shift @ARGV || die "$0 <filename>\n";
open( FH, "<$file" ) or die "cannot open file $!\n";
#MARK
$in_read = 0;
while( read( FH, $four_bytes, 4 ) > 0 ) {
last if( length( $four_bytes ) != 4 ); # EOF?
if( $four_bytes eq "MARK" ) {
print "buffer was ", $buffer, "\n" if $in_read;
$in_read = $in_read ? 0 : 1;
$buffer = "";
} else {
$buffer .= substr( $four_bytes, 0, 1 ) if $in_read;
# back up three bytes
$pos = tell(FH) - 3;
seek( FH, $pos, 0 );
}
}
# catch any trailing?
$buffer .= $four_bytes if $in_read;
# do something with the last buffer
print "dangling buffer: ", $buffer, "\n" if $buffer;
-derby
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.