[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Too many CR's on Windows

Erik Veenstra

6/22/2005 8:39:00 PM


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?

I'm a little bit confused now...

gegroet,
Erik V. - http://www.erikve...

----------------------------------------------------------------

# Test with PUTS

File.open("test.txt", "w") do |f|
f.puts "abc\r\n"
end

File.open("test.txt", "r") do |f|
puts f.read.length
end

system "dir test.txt"

----------------------------------------------------------------

# Test with WRITE

File.open("test.txt", "w") do |f|
f.write "abc\r\n"
end

File.open("test.txt", "r") do |f|
puts f.read.length
end

system "dir test.txt"

----------------------------------------------------------------

2 Answers

James Gray

6/22/2005 8:47:00 PM

0

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

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



Robert Klemme

6/22/2005 9:30:00 PM

0

Erik Veenstra <pan@erikveen.dds.nl> 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.
>
> 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.

Kind regards

robert

>
> I'm a little bit confused now...
>
> gegroet,
> Erik V. - http://www.erikve...
>
> ----------------------------------------------------------------
>
> # Test with PUTS
>
> File.open("test.txt", "w") do |f|
> f.puts "abc\r\n"
> end
>
> File.open("test.txt", "r") do |f|
> puts f.read.length
> end
>
> system "dir test.txt"
>
> ----------------------------------------------------------------
>
> # Test with WRITE
>
> File.open("test.txt", "w") do |f|
> f.write "abc\r\n"
> end
>
> File.open("test.txt", "r") do |f|
> puts f.read.length
> end
>
> system "dir test.txt"
>
> ----------------------------------------------------------------