How to get a newline when writing file from PC to Linux usin

I am trying to write a text file on Unix (via Samba) from a Ruby
program running on the PC. I am trying to get the end of line
correct. I cannot get the Unix newline character (0x0a) in the file.
I am getting the same results when writing to either the PC or Linux
so Samba is not the issue. How do you do this?

tf = “S:/open/test_ending.txt” # Unix
#tf = “C:/temp/test_ending.txt” # PC

fo = File.open(tf, “w”)
text = “this is line ending lf-r\r”
fo.write(text) # puts an OD (cr) at end of line
text = “this is line ending crlf\n”
fo.write(text) # puts an 0D0A at end of line
text = “this is line ending 0x0a”
fo.write(text)
fo.putc 0x0a # puts an 0D0A at end of line - Weird
text = “this is line ending 0x0d”
fo.write(text)
fo.putc 0x0d # puts an 0D at end of line
fo.close

I cannot get a linefeed (newline 0x0a) in the file.

results of writing to unix and pc are the same.

fo = File.open(tf, “w”)

Open your file in binary mode - and you will have what you expect.

El Jul 11, 2007, a las 3:18 PM, billbell52
escribió:

I am trying to write a text file on Unix (via Samba) from a Ruby
program running on the PC. I am trying to get the end of line
correct. I cannot get the Unix newline character (0x0a) in the file.
I am getting the same results when writing to either the PC or Linux
so Samba is not the issue. How do you do this?

“\n” to native newline I/O conversion depends on the runtime
platform, which in your case is Unix. In Unix no conversion is
needed, so if you print a “\n” in Unix that’s what you get (a single
character with octal code 012).

To output CRLF, the convention in your target platform, output “\015
\012” by hand.

The whole story is explained here

Radar – O’Reilly
newlines.html

it is Perl-based but what explains applies to Ruby as well (expect
perhaps for the corner case of Macs before OSX).

– fxn