[R] Generating the Ctrl-M character

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Fri Nov 11 07:41:49 CET 2011


No need to get defensive... I couldn't tell whether you remembered the details. This is starting to go OT, though.

As to handing "\n" as expected, in the context of generating text files for their native platforms they do act "as expected".  However, in the context of people writing binary characters in a file in an attempt to make it suitable for some other platform, munging can happen, and people can be surprised by the results if they don't get the whole picture.

Specifically, AFAIK "\n" is always stored as a single character (LF) in memory on UTF8 or ASCII systems. On Windows, when that character is written to a text file a "\r" is written just before it, creating the expected end-of-line marker for a text file on that OS.  On the old Macintosh operating system a  lone "\r" was the EOL marker, so "\n" was automatically translated to "\r" (but not on OSX, which uses UNIX conventions). From the perspective of the person working with native text files, this makes things "just work". From the perspective of someone trying to obtain a particular but pattern in the output file this amounts to "munging".

The C programming standard library includes the concept of distinguishing between text files and binary files just so this "native adaptation" can occur or not, depending on the desired behavior.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

David Winsemius <dwinsemius at comcast.net> wrote:

>
>On Nov 10, 2011, at 11:55 PM, Jeff Newmiller wrote:
>
>> Wow, Deadpan David.
>>
>> How about using the escape sequence "\r"?
>
>So shoot me. It wasn't documented in a manner that I recognized in the 
>
>places I looked.
>
>?character
>?Syntax
>?Constants
>
>And I did look at ?Quotes where "\r" is listed but did not know that  
>it was == cntrl-M (if in fact it is.) I assumed (probably incorrectly) 
>
>that ""\r" was cntrl-R. There was proabably a time in the past when I  
>could have told you a lot of the decimal and maybe even hexadecimal  
>equivalents for <cr>, <lf>, <beep>, but those days are behind me.
>
>>
>> Keep in mind that Ctrl-M is used as the end-of-line character on  
>> some operating systems, so accomplishing this may not be portable,  
>> and you didn't specify your operating system. On the three main  
>> platforms (*nix, Windows/DOS, and Mac), "\r" should work, but "\n"  
>> may get munged.
>
>What? I very much doubt that any of those systems will not handle "\n" 
>
>as expected. "\r" on the other hand I'm not so sure of.
>
>-- 
>David.
>
>
>>
>---------------------------------------------------------------------------
>> Jeff Newmiller                        The     .....       .....  Go  
>> Live...
>> DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.   
>> Live Go...
>>                                      Live:   OO#.. Dead: OO#..   
>> Playing
>> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
>> /Software/Embedded Controllers)               .OO#.       .OO#.   
>> rocks...1k
>>
>---------------------------------------------------------------------------
>> Sent from my phone. Please excuse my brevity.
>>
>> David Winsemius <dwinsemius at comcast.net> wrote:
>>
>>>
>>> On Nov 10, 2011, at 9:35 PM, Ashim Kapoor wrote:
>>>
>>>> Dear R-helpers,
>>>>
>>>> I want to append a Ctrl-M character to a string and then save it to
>>>> a text
>>>> file.
>>>>
>>>> mystring<-"This is a test."
>>>>
>>>> # How do I add a Ctrl-M to it in the end ??
>>>>
>>>> cat(mystring,file="testfile")
>>>>
>>>
>>>> cntrl_m <- intToUtf8(13)
>>>
>>>> cat(cntrl_m,file="testfile")
>>>
>>> The resulting file seems to have a "blank line" in my editor.
>>>
>>> -- 
>>>
>>> David Winsemius, MD
>>> West Hartford, CT
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>David Winsemius, MD
>West Hartford, CT



More information about the R-help mailing list