From murdoch.duncan at gmail.com  Tue Apr 24 19:48:13 2012
From: murdoch.duncan at gmail.com (Duncan Murdoch)
Date: Tue, 24 Apr 2012 13:48:13 -0400
Subject: [Rd] Write unix format files on windows and vice versa
In-Reply-To: <CAK_CNybknyMi0MaTqT7-FxWrkXe02fTPYZJXZYR0L=GOUffxpg@mail.gmail.com>
References: <CAK_CNybknyMi0MaTqT7-FxWrkXe02fTPYZJXZYR0L=GOUffxpg@mail.gmail.com>
Message-ID: <4F96E75D.2090305@gmail.com>

On 24/04/2012 1:23 PM, Andrew Redd wrote:
> I go back and forth between windows and linux, and find myself running
> into problem with line endings.  Is there a way to control the line
> ending conversion when writing files, such as write and cat?  More
> explicitly I want to be able to write files with LF line endings
> rather than CRLF line ending on windows; and CRLF line endings instead
> of LF on linux, and I want to be able to control when the conversion
> is made and/or choose the line endings that I want.
>
> As far as I can tell the conversion is not optional and buried deep in
> compiled code. Is there a possible work around?

On Windows you will write CRLF if the file was opened as a text file, 
and LF only if it was opened as a binary file.  For example:

writeLines(letters, "crlf.txt")
con <- file("lf.txt", open="wb")
writeLines(letters, con)
close(con)

On Unix you can write CRLF by specifying that as the sep arg.  So the 
following will work on both systems:

con <- file("crlf.txt", open="wb")
writeLines(letters, con, sep="\r\n")
close(con)

(and the explicit connection isn't really necessary on Unix, but it is 
on Windows).

If you're not using the writeLines() function, it's probably a bit 
harder to set CRLF as a default on Unix than it is to set LF as a 
default on Windows.

Duncan Murdoch



[Rd] Write unix format files on windows and vice versa
Duncan Murdoch