So, I am attempting to read all binary files in a directory, seek to a header section, advance the position 94 bytes, and then change the next 10 bytes. With all of my research I have gotten close with the code below but syswrite is looking for numeric information. I should probably also be performing the replacement on a copy of the file but it could be added later. Anyone who knows the simplest method of replacing the 10 bytes in the syswrite statement has my appreciation and thanks.

#!/usr/bin/perl use strict; use warnings; use Data::HexDump; my $tktdir = "/home/bkirch/PERL_LSN/TICKETS"; my $file; my $offset; opendir(TKS, $tktdir) || die "Oops ... $!"; my @files = readdir TKS; close TKS; foreach my $file (@files) { open(FH,"+<$tktdir/$file") or die "Oops - Cant open ticket $!"; binmode FH; while (<FH>) { if (/SCSI:INQ:80/) { $offset = tell (FH); # print "$offset\n"; #seek (FH, $offset, 94); seek (FH, $offset, 94); $offset = tell (FH); # print "$offset\n"; #read(FH, $SN, 10); sysread (FH, $offset, 10); # print "$offset\n"; my $BUF = ""; syswrite (FH, $BUF, 10, $offset); print "$BUF\n"; # s[$offset][TEST123456]g; } } }

Thanks JavaFan and graff for responding to my question. You can tell I am a beginner by the print debug statements in the code. When the code is run it checks the binary file for the pattern match and the first instance of "tell" actually places a pointer at the first instance of "S" in SCSI. seek moves the pointer forward 94 bytes and the second instance of tell resets the pointer at the beginning character of the data to be replaced. This is verified by the (commented out read statement when the program is run). In trying JavaFan's solution I run into errors (on Windows) with regard to the options used (-pi -e). I hope to run this routine on both Windows and Linux. I will keep trying but again thanks for your suggestions. -pbyfire


In reply to Changing data with syswrite ? by pbyfire

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.