Binary file reading in Ruby

R

rohit2000s

Hi All,

I am trying to read in a binary file of size 'n' using:

mem_buf = IO.read(file_name, n, 0)

Where file_name is an argument to the script.

But after the read I observed that the size of the mem_buf is less
than 'n' (It should be equal to 'n').

(mem_buf.size != n).

This script fails on the above read check.

Any ideas what I might be doing wrong here. Any help will be
appreciated.


Thanks in advance,
Rohit
 
A

Al Og

unknown said:
mem_buf = IO.read(file_name, n, 0)

Where file_name is an argument to the script.

But after the read I observed that the size of the mem_buf is less
than 'n' (It should be equal to 'n').

It looks like your file size is less than number of bytes you're trying
to read.
In this case size of the mem_buf must be equal to the file size.

mem_buf.size == File.size(file_name)

Regards,
arkadi4
 
R

Robert Klemme

2007/12/21 said:
Hi All,

I am trying to read in a binary file of size 'n' using:

mem_buf = IO.read(file_name, n, 0)

Where file_name is an argument to the script.

But after the read I observed that the size of the mem_buf is less
than 'n' (It should be equal to 'n').

(mem_buf.size != n).

This script fails on the above read check.

Any ideas what I might be doing wrong here. Any help will be
appreciated.

Either, the file is smaller than n or you have an issue with
conversion (typically on Windows). For binary files I'd do this which
is more portable:

mem_buf = File.open(file_name,"rb") {|io| io.read}

Also, it helps documenting things. Bw, you do not need the offset and
size arguments.

Kind regards

robert
 
R

rohit2000s

2007/12/21, (e-mail address removed) <[email protected]>:











Either, the file is smaller than n or you have an issue with
conversion (typically on Windows). For binary files I'd do this which
is more portable:

mem_buf = File.open(file_name,"rb") {|io| io.read}

Also, it helps documenting things. Bw, you do not need the offset and
size arguments.

Kind regards

robert

Thanks a lot ! That was the exact problem I had - getting that part of
code to work on windows.

Could you please take time to detail a bit on:

"{|io| io.read}" part of the code.

Also is there a way to add the offset and size arguments to the read
method that you suggested ?

Thanks in advance,
Rohit
 
R

Robert Klemme

Thanks a lot ! That was the exact problem I had - getting that part of
code to work on windows.

You probably should have mentioned this. :)
Could you please take time to detail a bit on:

"{|io| io.read}" part of the code.

It's the block form of File.open and the return value of File.open in
this case is the result of the block.
Also is there a way to add the offset and size arguments to the read
method that you suggested ?

http://ruby-doc.org/core/classes/IO.html#M002295

Cheers

robert
 

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
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top