Too many CR's on Windows

E

Erik Veenstra

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.erikveen.dds.nl/

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

# 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"

----------------------------------------------------------------
 
J

James Edward Gray II

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
 
R

Robert Klemme

Erik Veenstra said:
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
 

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
474,174
Messages
2,570,940
Members
47,486
Latest member
websterztechnologies01

Latest Threads

Top