Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: how to read binary file and give a binary output?

by mr_mischief (Monsignor)
on Oct 16, 2014 at 17:46 UTC ( [id://1104092]=note: print w/replies, xml ) Need Help??


in reply to Re: how to read binary file and give a binary output?
in thread how to read binary file and give a binary output?

This makes the assumption that the file fits in RAM. This is usually, but not always, a safe assumption. Things may get complicated if that's not the case. Since we're not talking about line-oriented text files read (and possibly sysread and syswrite) would be good additional reading.

If there are fixed-length records there are ways to deal with that. If there are record terminators consistent throughout the file, there's a way to deal with that, too. See $/ for more information.

  • Comment on Re^2: how to read binary file and give a binary output?

Replies are listed 'Best First'.
Re^3: how to read binary file and give a binary output?
by Loops (Curate) on Oct 17, 2014 at 03:14 UTC

    Good point about it all having to fit into RAM. Should be relatively straightforward to read byte by byte, although looking ahead for the two byte separator would need some finagling. hmmm, how about:

    my $ifilename = 'file.in'; my $ofilename = $ifilename . '.bin'; open my $ifile, '<', $ifilename or die "could not open input file $!"; open my $ofile, '>', $ofilename or die "could not open output file $!" +; binmode $ifile; binmode $ofile; my $n = eof($ifile) ? "" : getc($ifile); while ((my $c = $n) ne "") { $n = eof($ifile) ? "" : getc($ifile); print $ofile "$c$n" eq "\x4F\xFF" .. 1 ? $c : "\x0"; } close $ifile; close $ofile;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1104092]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-24 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found