how to write a "od -bc" like function?

J

jck11

hi all
Could any one give me a direction to write a function to match the unix-like
function "od -bc" do.
Now I only get the idea of opening the file.

open(IN, "filename")or die;
my $str=<IN>;
..............
close(IN);

By the way, if the file is a exe or image type, the $str seems don't work
correctly.
 
A

A. Sinan Unur

hi all
Could any one give me a direction to write a function to match the
unix-like function "od -bc" do.
Now I only get the idea of opening the file.

open(IN, "filename")or die;

Prefer the three-argument form of open.
At least give an indication why the call failed.

open my $in, '<', 'filename'
or die $!;
my $str=<IN>;


Here, you read only one line from the file.

.............
close(IN);

By the way, if the file is a exe or image type, the $str seems don't
work correctly.

Study my hexdump example at:

http://www.unur.com/comp/ppp/hexdump.html

You should be able to adapt that to produce the same output od -bc
produces by changing only the hexdump_line subroutine.

Keep in mind:

You must read the input file in binary mode.

Line-by-line processing does not mean much here as you are trying to
display fixed sized blocks of bytes.


Sinan
 
J

Josef Moellers

jck11 said:
hi all
Could any one give me a direction to write a function to match the unix-like
function "od -bc" do.
Now I only get the idea of opening the file.

open(IN, "filename")or die;
my $str=<IN>;
.............
close(IN);

By the way, if the file is a exe or image type, the $str seems don't work
correctly.

Apart from the usual: three-argument "open", lexical filehandles, and
explicit '<':
1. if you're dealing with binary files, you must tell perl this:
"binmode IN;" after open(...), and
2. you're not reading "strings" of *characters* but "blocks" of *binary*
data, so better use "read(...)".

so
perldoc -f binmode
perldoc -f read
 
J

John W. Krahn

Josef said:
Apart from the usual: three-argument "open", lexical filehandles, and
explicit '<':
1. if you're dealing with binary files, you must tell perl this:
"binmode IN;" after open(...), and

Or use the open pragma:

use open IO => ':raw';



perldoc open



John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top