Read files at the byte level - how to?

R

RLMuller

This is a multi-part message in MIME format.

------=_NextPart_000_0091_01C3759A.1DBD6270
Content-Type: text/plain;
charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable

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)

------=_NextPart_000_0091_01C3759A.1DBD6270
Content-Type: text/html;
charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Dwindows-1252">
<META content=3D"MSHTML 6.00.2800.1226" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT size=3D2>Hi All,</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>I want to use Ruby to compare two Zip files to test =
whether=20
they are identical.&nbsp; Accordingly,&nbsp; I want to read at the byte =
level,=20
but I only see methods to read strings, i.e. gets, readline and=20
readlines.</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>Is there a way to do what I want to do?&nbsp; Is =
there an=20
example somewhere on the Web?</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>Thanks in advance.</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>Regards,<BR>Richard</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT size=3D2>A programmer is a device for turning coffee into =
code.<BR>Jeff=20
Prosise (with an assist from Paul Erdos)<BR></FONT></DIV></BODY></HTML>

------=_NextPart_000_0091_01C3759A.1DBD6270--
 
N

nobu.nokada

Hi,

At Mon, 8 Sep 2003 12:47:01 +0900,
RLMuller said:
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)
 
G

Gavin Sinclair

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
 

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,776
Messages
2,569,603
Members
45,197
Latest member
Sean29G025

Latest Threads

Top