# Force file write in dos format

**URL:** <https://www.ruby-forum.com/t/force-file-write-in-dos-format/113545>\
**Category:** Ruby\
**Created:** [September 1, 2007, 1:17pm UTC](https://www.ruby-forum.com/t/force-file-write-in-dos-format/113545 "2007-09-01T13:17:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [September 1, 2007, 1:17pm UTC](https://www.ruby-forum.com/t/force-file-write-in-dos-format/113545/1 "2007-09-01T13:17:46Z")

</div>

Is there any built-in way to force File.puts to use dos line endings,  
regardless of the platform under which the program is running?

martin

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [September 1, 2007, 2:37pm UTC](https://www.ruby-forum.com/t/force-file-write-in-dos-format/113545/2 "2007-09-01T14:37:57Z")

</div>

> 

Use IO#print instead of IO#puts and set $, the default output record  
separator:

## --------------------------------------------------------------- IO#print ios.print() =\> nil ios.print(obj, …) =\> nil

```
 Writes the given object(s) to _ios_. The stream must be opened for
 writing. If the output record separator (+$\+) is not +nil+, it
 will be appended to the output. If no arguments are given, prints
 +$_+. Objects that aren't strings will be converted by calling
 their +to_s+ method. With no argument, prints the contents of the
 variable +$_+. Returns +nil+.

    $stdout.print("This is ", 100, " percent.\n")

 _produces:_

    This is 100 percent.

```

irb(main):001:0\> $\ = “\nanother line\n”  
=\> “\nanother line\n”  
irb(main):002:0\> print “test”  
test  
another line  
=\> nil  
irb(main):003:0\>

HTH,

Felix

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [September 1, 2007, 2:42pm UTC](https://www.ruby-forum.com/t/force-file-write-in-dos-format/113545/3 "2007-09-01T14:42:57Z")

</div>

On 9/1/07, Felix W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Use IO#print instead of IO#puts and set $, the default output record  
> separator:

Thanks!

martin

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [September 2, 2007, 2:53am UTC](https://www.ruby-forum.com/t/force-file-write-in-dos-format/113545/4 "2007-09-02T02:53:08Z")

</div>

On Sep 1, 2007, at 1:16 PM, Martin DeMello wrote:

> Is there any built-in way to force File.puts to use dos line endings,  
> regardless of the platform under which the program is running?

If I undertand correctly what you want your program is going to  
explicitly output pairs “\r\n”.

If that’s the case, in addition to the recommendation about IO#print  
already given take into account that the io needs binmode. Otherwise,  
on CRLF platforms you’d end up with “\r\r\n” on disk, which would be  
wrong. That’s because the single “\n” in “\r\n” gives itself a pair  
“\r\n” in text mode. So IO#print CRLF in binmode is the complete  
portable solution.

– fxn

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [September 2, 2007, 10:18pm UTC](https://www.ruby-forum.com/t/force-file-write-in-dos-format/113545/5 "2007-09-02T22:18:15Z")

</div>

On 9/2/07, Xavier N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> on CRLF platforms you’d end up with “\r\r\n” on disk, which would be  
> wrong. That’s because the single “\n” in “\r\n” gives itself a pair  
> “\r\n” in text mode. So IO#print CRLF in binmode is the complete  
> portable solution.

Good point. As of now this will only ever be run on a linux box, but  
it certainly doesn’t hurt to future-proof it.

martin
