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 }
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 <ralph@lkjlkj.com> writes:
On Tue, 25 Oct 2005 17:53:22 +0900, Yukihiro Matsumoto <matz@ruby-lang.org> 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 <ralph@lkjlkj.com> writes:
this comes up so often - would it make sense to be the default mode on
windows?
-a
···
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 <ralph@lkjlkj.com> writes:
>f = File.new(fname)
Try
f = File.new(fname, "rb")
and see how it works.
--
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
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.Howard@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.
> 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 <ralph@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
>> 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
···
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.Howard@noaa.gov> writes:
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
···
On 10/25/05, Michael Campbell <michael.campbell@gmail.com> wrote:
"Ara.T.Howard" <Ara.T.Howard@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 <ralph@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?
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.
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.
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.