force file write in dos format

Discussion in 'Ruby' started by Martin DeMello, Sep 1, 2007.

  1. 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
     
    Martin DeMello, Sep 1, 2007
    #1
    1. Advertising

  2. On 9/1/07, Felix Windt <> wrote:
    >
    > Use IO#print instead of IO#puts and set $\, the default output record
    > separator:


    Thanks!

    martin
     
    Martin DeMello, Sep 1, 2007
    #2
    1. Advertising

  3. Martin DeMello

    Xavier Noria Guest

    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
     
    Xavier Noria, Sep 2, 2007
    #3
  4. On 9/2/07, Xavier Noria <> wrote:
    > 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.


    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
     
    Martin DeMello, Sep 2, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Fairbank
    Replies:
    2
    Views:
    5,460
  2. john san
    Replies:
    19
    Views:
    743
    Diez B. Roggisch
    Feb 18, 2005
  3. Replies:
    5
    Views:
    720
    Magnus Lycka
    Oct 27, 2006
  4. tomhr
    Replies:
    27
    Views:
    1,421
    Mike Wahler
    Jan 12, 2006
  5. mmccaws2
    Replies:
    9
    Views:
    187
    Joost Diepenmaat
    Apr 9, 2008
Loading...

Share This Page