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

R

Ralph Smith

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
 
Y

Yukihiro Matsumoto

Hi,

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

|f = File.new(fname)

Try

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

and see how it works.


matz.
 
A

Ara.T.Howard

Hi,

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

|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?

-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
===============================================================================
 
M

Michael Campbell

Ara.T.Howard said:
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?
 
Y

Yukihiro Matsumoto

Hi,

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

|> 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.
 
P

Pit Capitain

Yukihiro said:
Hi,

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

|> 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.

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
 
P

Pit Capitain

Sean said:
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.

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
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top