Beginner wrote:
Hi,
When I ask for the filehandle position (tell) it is reporting the
pointer as being a few bytes further along than I expect it to be.
For instance if I do this:
#!/bin/perl
use strict;
use warnings;
my ($start,$d,$var);
my $file = 'myjpeg.jpg';
open(FH,$file) or die "Can't open $file: $!\n";
binmode(FH);
while (<FH>) {
if ($_ =~ /\xFF\xC0/) { # Start of frame header.
print "Found start of frame at $start\n";
seek(FH,$start,0);
my $read = read(FH,$var, 2);
print unpack("H*", $var), "\n";
# This should return ff C0 or at least the next 2 bytes
print "read $read characters at $start and got $var\n";
}
}
It tells me that it is reading at 7345. Yet when I open the same file
in a hex editor and search for 'ff c0' it reports the position as
7290.
Is this because it is reading the file in line by line? Setting $/;
doesn't change the output. Can anyone explain this?
This code didn't tell you it was reading at 7345 as $start is never
assigned, and the way you're assigning it is what is causing the problem.
Something like the program below should work.
HTH,
Rob
use strict;
use warnings;
my $file = 'myjpeg.jpg';
open(FH,$file) or die "Can't open $file: $!\n";
binmode(FH);
my $offset = 0;
while (<FH>) {
next unless /\xFF\xC0/g;
my $start = $offset + pos() - 2;
print "Found start of frame at $start\n";
seek FH, $start, 0;
my $read = read FH, my $var, 2;
print unpack("H*", $var), "\n";
# This should return ff C0 or at least the next 2 bytes
print "read $read characters at $start and got $var\n";
}
continue {
$offset = tell FH;
}
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/