Line Ending Confusion on Windows

Z

Zach Dennis

I am testing some various line endings and conversions. I am confused
because when I create a file using DOS (CR/LF) format and I do a simple:

fh = File.open( "myfile.txt" );
fh.each_byte{ |ch|
puts ch; end

I get the following output:

72
101
108
108
111
44
10
87
111
114
108
100
33
10

myfile.txt contains:

Hello,
World!


By reading this output you would think it is in Unix format (LF), but it is
actually in DOS (CR/LF) file format.
When I open the file up in a hexeditor (NitroHex) it shows that the file
format is actually in DOS (CR/LF). I am using Ruby 1.8.1preview3 on Windows
2000. Is the Ruby Interpretor screwing up my line endings?

This actually came up from because I was trying what was suggest by a March
post by "Josef 'Jupp' Schugt" about any to any conversions for line endings.
But it doesn't work on my Windows box.

Any input is appreciated,

Zach Dennis
 
F

Florian Gross

Zach said:
By reading this output you would think it is in Unix format (LF), but it is
actually in DOS (CR/LF) file format.

Ruby automatically converts platform-specific (\r\n in your case)
newlines to platform-independent newlines (\n). This is a feature.

If you don't want any line ending conversations to happen just specify
the binary mode like this: File.open("filename", "rb") { ... }

Regards,
Florian Gross
 

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

Forum statistics

Threads
474,262
Messages
2,571,043
Members
48,769
Latest member
Clifft

Latest Threads

Top