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

**URL:** <https://www.ruby-forum.com/t/how-to-get-a-newline-when-writing-file-from-pc-to-linux-usin/109103>\
**Category:** Ruby\
**Created:** [July 11, 2007, 3:29pm UTC](https://www.ruby-forum.com/t/how-to-get-a-newline-when-writing-file-from-pc-to-linux-usin/109103 "2007-07-11T15:29:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![billbell52](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ce73a5/32.png) [@billbell52](https://www.ruby-forum.com/u/billbell52)\
**Post date:** [July 11, 2007, 3:29pm UTC](https://www.ruby-forum.com/t/how-to-get-a-newline-when-writing-file-from-pc-to-linux-usin/109103/1 "2007-07-11T15:29:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![billbell52](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ce73a5/32.png) [@billbell52](https://www.ruby-forum.com/u/billbell52)\
**Post date:** [July 11, 2007, 3:47pm UTC](https://www.ruby-forum.com/t/how-to-get-a-newline-when-writing-file-from-pc-to-linux-usin/109103/2 "2007-07-11T15:47:52Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![billbell52](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ce73a5/32.png) [@billbell52](https://www.ruby-forum.com/u/billbell52)\
**Post date:** [July 11, 2007, 4:23pm UTC](https://www.ruby-forum.com/t/how-to-get-a-newline-when-writing-file-from-pc-to-linux-usin/109103/3 "2007-07-11T16:23:01Z")

</div>

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](http://www.onlamp.com/pub/a/onlamp/2006/08/17/understanding-)  
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
