Open the file RW ('+<'. See perlopentut for more info.)
Use sysseek to move to the byte ("bit") of interest.
Use syswrite (syswrite FH, $bitvalue, 1;) to write the "bit" in question.
#! perl -slw
use strict;
use Fcntl qw[ SEEK_SET SEEK_CUR SEEK_END ];
open OUT, '> :raw', 'test.dat' or die $!;
print OUT 'descriptor';
print OUT '1' x 1000;
close OUT;
my $descOffset = length( 'descriptor' ) + 2; ## May vary with OS defin
+ition of "\n";
open RW, '+< :raw', 'test.dat' or die $!;
for( my $bitPos = 0,; $bitPos < 1000; $bitPos += 2 ) {
## Read bit[ $bitPos ]
sysseek RW, $descOffset + $bitPos, SEEK_SET;
my $bit;
sysread RW, $bit, 1;
print "bit[ $bitPos ] = $bit";
## Update bit[ $bitPos ]
sysseek RW, $descOffset + $bitPos -1, SEEK_SET;
## -1 gives zero-based bit positions.
syswrite RW, '0', 1;
}
close RW;
Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Time is a poor substitute for thought"--theorbtwo
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
|