I am trying to open files, seek to particular offsets, and read the byte value at that offset. My input file is the output of a number of executions of the Windows command FC /B which does a binary comparison of two files. I want to pick up the second filename (this works), seek to the offset mentioned in the driver file, read the byte at that position, and print out the hex value. Here's my code, and an example input file:
my $buf = ' '; while (<>) { if (/Comparing files (.+) and (.+)/) { open FH,">>$2" or die "Can't open $2\n"; binmode FH; print "Opening $2\n"; } if (/([0-9A-F]{8}): ([0-9A-F]{2}) ([0-9A-F]{2})$/ and $3 gt $2) { sysseek FH,hex($1),0; print "Seek $1 (".hex($1).")\n"; sysread FH,$buf,1; print "Read ".sprintf("%2.2x",ord($buf))."\n"; } } __END__ Comparing files C:\PROGRAM FILES\PROJECT ENTROPIA\DATA\MODELS\Models.b +nt and C:\DATA\MODELS\MODELS.BNT 05063755: 03 43 0875CF55: BF FF 09DC0155: 00 40 0B209955: 40 00 0DE8AD55: 0A 4A 0DF4ED55: 7F 3F Comparing files C:\PROGRAM FILES\PROJECT ENTROPIA\DATA\TERRAIN\50.bnt +and C:\DATA\TERRAIN\50.BNT 03040155: 15 55 Comparing files C:\PROGRAM FILES\PROJECT ENTROPIA\DATA\TEXTURES\Textur +es.bnt and C:\DATA\TEXTURES\TEXTURES.BNT 088C1355: 4C 0C 10F45955: 40 00 1155A355: 1E 5E 181FDB55: A6 E6
It always prints out 20, which is the space that I am initializing $buf with. What am I doing wrong?

In reply to File reading with sysseek and sysread by PhilHibbs

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.