If I run these scripts on Linux (replacing "dir" with "ls -l")
the outputs are what I expect. Both scripts print 5 and show a
file size of 5. But when I run them on Windows... Surprise,
surprise... The file sizes become 6! A hex editor tells me that
there are 2 CR's: 61 62 63 0D 0D 0A.
In Ruby (and Perl), \n is symbolic in meaning, representing the platforms standard line ending. Thus on Windows, \n translates to \r\n. You are adding an extra \r.
You can shut off this translation behavior by opening the file in "binmode" (add a "b" to the file mode string).
Hope that helps.
James Edward Gray II
···
On Jun 22, 2005, at 3:40 PM, Erik Veenstra wrote:
If I run these scripts on Linux (replacing "dir" with "ls -l")
the outputs are what I expect. Both scripts print 5 and show a
file size of 5. But when I run them on Windows... Surprise,
surprise... The file sizes become 6! A hex editor tells me that
there are 2 CR's: 61 62 63 0D 0D 0A.
If I run these scripts on Linux (replacing "dir" with "ls -l")
the outputs are what I expect. Both scripts print 5 and show a
file size of 5. But when I run them on Windows... Surprise,
surprise... The file sizes become 6! A hex editor tells me that
there are 2 CR's: 61 62 63 0D 0D 0A.
Why?
puts automatically appends the platforms line termination unless the string does already contain a line termination. Normally you just to puts "abc" and if you need multiple line breaks puts "abc\n\n\n".
As james said, you can use binmode ("wb" for writing) to print literal "\n". As a rule of thumb: for textfiles one typically uses gets for reading and puts/print/printf for writing; for binary files read and write are usually used.