[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Read files at the byte level - how to?

RLMuller

9/8/2003 3:47:00 AM

Hi All,

I want to use Ruby to compare two Zip files to test whether they are identical. Accordingly, I want to read at the byte level, but I only see methods to read strings, i.e. gets, readline and readlines.

Is there a way to do what I want to do? Is there an example somewhere on the Web?

Thanks in advance.

Regards,
Richard

A programmer is a device for turning coffee into code.
Jeff Prosise (with an assist from Paul Erdos)
2 Answers

nobu.nokada

9/8/2003 3:53:00 AM

0

Hi,

At Mon, 8 Sep 2003 12:47:01 +0900,
RLMuller wrote:
> I want to use Ruby to compare two Zip files to test whether
> they are identical. Accordingly, I want to read at the byte
> level, but I only see methods to read strings, i.e. gets,
> readline and readlines.

require ''fileutils''
FileUtils.cmp(file_a, file_b)

Or

require ''ftools''
File.cmp(file_a, file_b)

--
Nobu Nakada

Gavin Sinclair

9/8/2003 4:30:00 AM

0

> Hi All,
>
> I want to use Ruby to compare two Zip files to test whether they are
> identical. Accordingly, I want to read at the byte level, but I only
> see methods to read strings, i.e. gets, readline and readlines.
>
> Is there a way to do what I want to do? Is there an example somewhere
> on the Web?

To get the whole file in one string:

bytes = File.open("filename", "rb") { |file| file.read }

Sadly, you can''t do:

bytes = File.read("filename", "rb") # File::read assumes text mode

Anyway, "bytes" is now a string with the whole file in it. You can, of
course, read a certain number of bytes with "file.read(1024)" for
instance.

Sorry for any misinformation in this reply; it''s based on experience, not
knowledge ;)

Gavin