Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ruby gives different answer for checksum of files on windows and FreeBSD?

0 views
Skip to first unread message

Ralph Smith

unread,
Oct 25, 2005, 4:35:30 AM10/25/05
to

If I run this code to compute a "checksum" of a file I get a different answer on a windows machine
and a FreeBSD machine. Does anyone know why? Or a better way to get a quick checksum of a file?

fname = ARGV[0]

size = File.size(fname);
checksum = 0;

f = File.new(fname)
f.each_byte {|x| checksum += x }

printf("%s: %d ( %x hex )\n", fname, checksum, checksum)


Thanks,
Ralph

Yukihiro Matsumoto

unread,
Oct 25, 2005, 4:53:22 AM10/25/05
to
Hi,

In message "Re: ruby gives different answer for checksum of files on windows and FreeBSD?"
on Tue, 25 Oct 2005 17:37:02 +0900, Ralph Smith <ra...@lkjlkj.com> writes:

|f = File.new(fname)

Try

f = File.new(fname, "rb")

and see how it works.


matz.


Ralph Smith

unread,
Oct 25, 2005, 8:19:10 AM10/25/05
to

that works. Thanks.

Ara.T.Howard

unread,
Oct 25, 2005, 10:21:21 AM10/25/05
to

this comes up so often - would it make sense to be the default mode on
windows?

-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| anything that contradicts experience and logic should be abandoned.
| -- h.h. the 14th dalai lama
===============================================================================

Michael Campbell

unread,
Oct 25, 2005, 11:02:01 AM10/25/05
to
"Ara.T.Howard" <Ara.T....@noaa.gov> writes:

> On Tue, 25 Oct 2005, Yukihiro Matsumoto wrote:
>
> > Hi,
> >
> > In message "Re: ruby gives different answer for checksum of files on windows and FreeBSD?"
> > on Tue, 25 Oct 2005 17:37:02 +0900, Ralph Smith <ra...@lkjlkj.com> writes:
> >
> > |f = File.new(fname)
> >
> > Try
> >
> > f = File.new(fname, "rb")
> >
> > and see how it works.
>
> this comes up so often - would it make sense to be the default mode on
> windows?

I'm guessing here, but my guess is that reading text files with ruby
is more common than non-text files. Does "rb" affect that?

--
I tend to view "truly flexible" by another term: "Make everything
equally hard". -- DHH

Yukihiro Matsumoto

unread,
Oct 25, 2005, 11:21:40 AM10/25/05
to
Hi,

In message "Re: ruby gives different answer for checksum of files on windows and FreeBSD?"

on Tue, 25 Oct 2005 23:21:21 +0900, "Ara.T.Howard" <Ara.T....@noaa.gov> writes:

|> f = File.new(fname, "rb")
|>
|> and see how it works.
|
|this comes up so often - would it make sense to be the default mode on
|windows?

Unless your program communicates with other programs on Windows, that
use text-mode.

matz.


Pit Capitain

unread,
Oct 25, 2005, 11:33:13 AM10/25/05
to
Yukihiro Matsumoto schrieb:

Could someone provide an example for such a program? My Ruby programs
communicate with Vim and SQL*Plus, but I haven't used text-mode yet.

Regards,
Pit


Austin Ziegler

unread,
Oct 25, 2005, 12:03:52 PM10/25/05
to
On 10/25/05, Michael Campbell <michael....@gmail.com> wrote:
> "Ara.T.Howard" <Ara.T....@noaa.gov> writes:
>> On Tue, 25 Oct 2005, Yukihiro Matsumoto wrote:
>>> Hi,
>>> In message "Re: ruby gives different answer for checksum of files on
>>> windows and FreeBSD?" on Tue, 25 Oct 2005 17:37:02 +0900, Ralph
>>> Smith <ra...@lkjlkj.com> writes:
>>>| f = File.new(fname)
>>> Try
>>>
>>> f = File.new(fname, "rb")
>>>
>>> and see how it works.
>> this comes up so often - would it make sense to be the default mode
>> on windows?
> I'm guessing here, but my guess is that reading text files with ruby
> is more common than non-text files. Does "rb" affect that?

Some. I *always* open files in Ruby with "rb" or "wb" on Windows. Here's
the difference, though:

irb(main):001:0> File.open("test.txt", "w") { |ff|
irb(main):002:1* ff.write <<-EOS
irb(main):003:1" Now is the time
irb(main):004:1" for all good people
irb(main):005:1" to rock.
irb(main):006:1" EOS
irb(main):007:1> }
=> 47
irb(main):008:0> a = File.open("test.txt", "r") { |ff| ff.read }
=> "Now is the time\n for all good people\nto rock.\n"
irb(main):009:0> b = File.open("test.txt", "rb") { |ff| ff.read }
=> "Now is the time\r\n for all good people\r\nto rock.\r\n"

Note that a has only \n and b has \r\n. With "r", Ruby translates away
the \r\n to \n only; with "rb" you have to look out for it yourself.

A simple #chomp (or #chomp!) operation, however, will eliminate both \n
and \r\n.

-austin
--
Austin Ziegler * halos...@gmail.com
* Alternate: aus...@halostatue.ca


daz

unread,
Oct 25, 2005, 5:12:29 PM10/25/05
to

Pit Capitain wrote:
> Yukihiro Matsumoto schrieb:

> >
> > Unless your program communicates with other programs on Windows, that
> > use text-mode.
>
> Could someone provide an example for such a program? My Ruby programs
> communicate with Vim and SQL*Plus, but I haven't used text-mode yet.
>


Notepad.

Many text editors these days are smart enough to pick out
text files in unix format and display them in lines rather
than one long line as Notepad ("correctly") does.
(If there are no "\r\n" sequences, there are no newlines.)

The smart editors give the illusion that your file has
been written correctly but the file on disk still has only
one line.

IBM's DOS Technical Reference[1] says that file I/O
always uses binary mode and cannot be changed.
(in contrast with device I/O - e.g. the console)

Windows CreateFile follows that lead - no binary/text
option AFAICS.

C's "fopen" seems to have missed this fact (??)

"If a t or b is not given in the mode string the mode
is governed by the global variable _fmode. If _fmode
is set to O_BINARY files are opened in binary mode.
If _fmode is set to O_TEXT they are opened in text mode.
These O_... constants are defined in fcntl.h."

_fmode defaults to O_TEXT (under Windows)

That looks to be inconsistent with the DOS/Windows
specification.

My interpretation is that STDIO should be in text mode
and all other files in binmode. That covers the cases
when STDIO is redirected - it'll be in text mode
which is usually what's required.

BTW, I don't think Ruby is at fault, here.
Changing now is likely to cause some problems.


daz


[1] Maybe not official(?) but it says the same as
an old copy I have. Two thirds down the page -
headed [ASCII and BINARY MODE]
http://www.textfiles.com/programming/dostech.pro

Sean O'Halpin

unread,
Oct 25, 2005, 5:36:55 PM10/25/05
to
On 10/25/05, daz <do...@d10.karoo.co.uk> wrote:
> C's "fopen" seems to have missed this fact (??)

It's part of ANSI C for stdio to open files by default in text mode.
This does nothing on Unix but it converts CRLF (\r\n) to LF (\n) on
Windows. This means you can write portable text processing programs in
C (and in Ruby). If you're handling binary, you have to know what
you're doing to write portable programs - binmode is the least of it.

Changing to binmode by default would cause a lot more confusion for
newbies and casual programmers on Windows. There'd be a lot of "how do
I get rid of this \r character?" posts for a start. I reckon a lot
more than binmode causes.

Regards,

Sean


Pit Capitain

unread,
Oct 26, 2005, 3:04:25 AM10/26/05
to
Sean O'Halpin schrieb:

Thanks to you and Daz for the detailed answer. So it seems that the
editors you normally use make a difference whether you prefer text or
binary mode.

Regards,
Pit


0 new messages