How to get a newline when writing file from PC to Linux using Samba

B

billbell52

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.
 
X

Xavier Noria

El Jul 11, 2007, a las 3:18 PM, billbell52 escribi=F3:
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 =20
platform, which in your case is Unix. In Unix no conversion is =20
needed, so if you print a "\n" in Unix that's what you get (a single =20
character with octal code 012).

To output CRLF, the convention in your _target_ platform, output "\015=20=

\012" by hand.

The whole story is explained here

http://www.onlamp.com/pub/a/onlamp/2006/08/17/understanding-=20
newlines.html

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

-- fxn
 

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
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top