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
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.