Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Reading binary files with Perl

0 views
Skip to first unread message

Apokrif

unread,
Aug 26, 2002, 2:25:31 PM8/26/02
to
I tried to read a file with a "while (<filehandler>)" loop, but the loop stops
when it reads a non-ASCII character (00H). Is there a way to read binary files,
like the "b" flag used with fopen in C ? I'd also like to know how to read one
character at a time, instead of reading a whole line.

John W. Krahn

unread,
Aug 26, 2002, 3:51:25 PM8/26/02
to
Apokrif wrote:
>
> I tried to read a file with a "while (<filehandler>)" loop, but the loop stops
> when it reads a non-ASCII character (00H).

00H IS ASCII.


> Is there a way to read binary files,
> like the "b" flag used with fopen in C ?

perldoc -f binmode


> I'd also like to know how to read one
> character at a time, instead of reading a whole line.

Setting the input record separator to a reference of a numeric literal.

$/ = \1; # reading from <filehandler> one character at a time

perldoc -f read
perldoc -f getc

John
--
use Perl;
program
fulfillment

Erik Knepfler

unread,
Aug 26, 2002, 4:49:37 PM8/26/02
to
"Apokrif" <apok...@yahoo.com> wrote in message
news:dfc75420.02082...@posting.google.com...

I can't say for certain but I've had horrible luck reading and writing
binary files on Windows without the following lines. Corrupted files and
general weirdness. No idea why it even affects it. Too lazy to
investigate. Here they are, good luck:

use CGI;
use CGI::Carp qw/fatalsToBrowser/;

I don't know if just use CGI::Carp would suffice. I'm happy this works and
don't change it, and those are really useful for what I'm doing anyway so I
don't consider it wasteful

Erik


David

unread,
Aug 26, 2002, 4:56:19 PM8/26/02
to
Try something like :

open(BINFILE, "</tmp/binfile.exe");
binmode(BINFILE); # tell perl the file handle is a binary file
local $/=undef; # undefined input separator so the whole file gets sucked in
my $complete_file = <BINFILE>;
close(BINFILE);

You will need to binmode() any file handle you intend to write this to as well.

Regards,
David


"Apokrif" <apok...@yahoo.com> wrote in message
news:dfc75420.02082...@posting.google.com...

Helgi Briem

unread,
Aug 27, 2002, 6:08:46 AM8/27/02
to
On Mon, 26 Aug 2002 13:49:37 -0700, "Erik Knepfler"
<ekne...@hotmail.com> wrote:

>I can't say for certain but I've had horrible luck reading and writing
>binary files on Windows without the following lines. Corrupted files and
>general weirdness. No idea why it even affects it. Too lazy to
>investigate. Here they are, good luck:
>
>use CGI;
>use CGI::Carp qw/fatalsToBrowser/;

No. These lines have nothing whatsoever to with binary
files, except in so far as all CGI scripts should have them
(and use them properly.

To read and write binary files on non-Unix systems, you want

the binmode function, see the docs for further info:

perldoc -f binmode
--
Regards, Helgi Briem
helgi AT decode DOT is

0 new messages