Brian McCauley said:
1- read in a file and store its binary (byte) contents into an array (in increments)
2- convert the binary bytes into their 8 bit 0/1 equivalents
I do not understand what you mean by this. On a computer[1] a file
consits of bytes and those bytes consist of 8 bits.
Perhaps if you gave an example of the desired input and output?
I want to capture the exact 0/1 bitstream from a datafile.
That does not clarify matters at all.
I suspect that what you think you are saying here is something more
than what you are actually saying. Try to take a step back and look
at what you are saying from the point of view of someone else. For a
scarily similar example of how someone can think they are asking a
perfectly clear question when actually they are not please see:
http://groups.google.com/[email protected]
How do I accomplish this ?
Well to answer the question you ask (as opposed to the question you
are trying to ask - of which I'm still in the dark)...
open() - to open the file
binmode() - to tell perl to treat the the file as binary data
read() - to read chunks of the file into a scalar variable used as a buffer.
use constant CHUNK => 1024;
my $filename='whatever';
open(my $fh, '<', $filename) or die $!;
binmode($fh);
{
my $r = read($fh, my $buffer, CHUNK);
die $! unless defined $r;
last unless $r;
# Do stuff with $buffer;
redo;
}
Examples like this should be easily found in any Perl tutorial or reference.
Actually in recent Perl open() and binmode() can be combined. For
details RTFM on open in 5.8.
You can also use readline() (aka the <> operator) rather than read().
For details on putting readline() into fixed size record mode (or
read-the-whole-file (aka "slurp") mode) RTFM on the special variable
$/.
See also:
File::Slurp.
FAQ: "How can I manipulate fixed-record-length files?"
FAQ: "How can I read in an entire file all at once?"
perldoc -f open
perldoc -f binmode
perldoc -f read
Posting guidelines for this newsgroup.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\