5

C でファイル内の 1 バイトを変更するのと非常に似ていますが、Cではなく Perl です。

Perl でバイナリ ファイルを開き、最初のバイトだけを変更して書き戻すにはどうすればよいですか?

4

2 に答える 2

12
open my $fh, '+<', $file      or die "open failed: $!\n";
my $byte;
sysread($fh, $byte, 1) == 1   or die "read failed: $!\n";
seek($fh, 0, 0);
syswrite($fh, $new_byte) == 1 or die "write failed: $!\n";
close $fh                     or die "close failed: $!\n"; 
于 2010-03-17T17:42:21.370 に答える
6

それを行う多くの方法。効率的な方法は、次のようにランダム アクセス モードでファイルを開くことですopen $fh, '+<'

my $first_byte = chr(14);      # or whatever you want the first byte to be
open my $fh, '+<', $the_file;
seek $fh, 0, 0;                # optional - cursor is originally set to 0
print $fh $first_byte;         # could also use  write  or  syswrite  functions
close $fh;
于 2010-03-17T17:42:36.883 に答える