420

I just can't remember those. So, what is the right way to properly terminate old fashioned ASCII lines?

Daniel Mošmondor
  • 19,070
  • 12
  • 53
  • 95
  • 12
    If you do this make sure you open your files in **binary mode**. Otherwise the standard C/C++ file streams will convert the "\n" (for text files (the default)) into a platform specific end of line sequence (ELS). Thus "\r\n" on windows will convert to "\r\r\n" on the physical disk. Note: if you just use C/C++ to read the file back you will not notice as the ELS be converted back to "\n" when read from the file. – Martin York Jun 30 '11 at 19:46
  • @Martin - I am in .net, and am using (for this example) File.AppendAllText, with "\r\n", and I just took a look into a file, and there is no \r duplication at the end of the line. Magic? – Daniel Mošmondor Jun 30 '11 at 19:54
  • @Daniel: I should have been more specific. The **C/C++** streams. When **associated with a file** and opened in **text mode**. It also depends on how you look at the file (use a hexeditor). – Martin York Jun 30 '11 at 20:12
  • @Martin - used PSPad in hex mode... – Daniel Mošmondor Jun 30 '11 at 20:17
  • @Daniel: You did notice I did not mention .net! – Martin York Jun 30 '11 at 20:48
  • 1
    On ancient keyboards, the Enter key was called the Return key. So the Return came first, then the Linefeed was generated in response. It's all so obvious when you have way too much history. – Mark Ransom Apr 21 '16 at 16:58

10 Answers10

1160

I'd use the word 'return' to remember, the r comes before the n.

Tomas Kubes
  • 20,134
  • 14
  • 92
  • 132
David Snabel-Caunt
  • 56,156
  • 12
  • 108
  • 132
  • 123
    And to remember that the word to use is *return*, remember that \r is the *carriage return*. Don't remember that "Return" is the name of the key on Mac keyboards, or else you might later look at a PC keyboard, see the key called "Enter," and get the letter order wrong! – Rob Kennedy Jun 30 '11 at 19:36
  • 3
    @David But I was wondering that effect of `\n\r` and `\r\n` is same, ain't they do same effect? can you tell the difference? – Mr.Anubis Jan 11 '12 at 15:56
  • 7
    @Mr.Anubis if you have _certain_ dot matrix printer, there should be NO difference, since the order of commands shouldn't be important. However, on some printers, you'll get extra empty line, or no line advancement at all. And everthing said above applies to text editors - they will be confused, TextStream classes will get sick, and so on. Order *IS* important. :) – Daniel Mošmondor Jan 31 '12 at 15:04
  • @DanielMošmondor Thanks for help:) – Mr.Anubis Jan 31 '12 at 15:30
  • 13
    @DanielMošmondor Alternatively, you could just remember that order is impoRtaNt. Or that when you asked at SO someone gave you a RemiNder, which you later forgot. Or that you _can't_ use your surname as a reminder, because the order of r and n there is reversed :) – Daniel Daranas Apr 25 '13 at 15:11
  • 5
    @RobKennedy haha you know I came here and read about return, and thought "great!". Then I read your comment. Now every time I can't remember which key I shouldn't be looking at - I look at my "Enter" key and I think "something's very wrong here"... haha over thinking it! – Wayne Uroda May 20 '13 at 01:20
  • @david, the simple sentence you said is the voice in of my head in past year. It helped me a lot. I wanted to thank you!!! – Allan Xu Mar 20 '17 at 17:34
  • 1
    And a way to remember which *not* to do: NeveR n before r. – Andrew Myers Apr 21 '17 at 17:49
  • I noticed a difference between \r\n and \n\r \n\r was giving me a blank line after the content i was breaking from. \r\n didnt give me a blank line it just broke to the next line. interesting. This was observed in a textarea – ModS Mar 07 '18 at 10:54
  • Good idea but had go back to this answer because I didn remeber the word, trying "new line" and "word" haha. But I think another option is r for carriage retrun, and n for new line, while logically makes sense to first init new line \n and then return \r to begging of that line. Please don't use MY WRONG advice :) – Pawel Cioch Apr 11 '18 at 14:19
55

If you are using C# you should use Environment.NewLine, which accordingly to MSDN it is:

A string containing "\r\n" for non-Unix platforms, or a string containing "\n" for Unix platforms.

BrunoLM
  • 88,362
  • 76
  • 272
  • 427
  • 47
    Actually this kind of depends on what you're doing. If you're just outputting plain text to the console or into a file for local use, `Environment.NewLine` is fine. However, if you're writing things in a well-defined format, the format may define the newline as well. For instance, the HTTP protocol states you **must** use `\r\n` as the line terminator for headers etc, regardless of platform. – Matti Virkkunen Jun 30 '11 at 19:28
  • 3
    Yea, this is really bad advice. – IvanP Dec 28 '15 at 13:16
  • This is why you need to escape your web messages and build unit tests to verify sending \r\n (windows), \r (mac), \n (linux) sends properly as \r\n to the webserver. – TamusJRoyce Aug 23 '17 at 15:42
42

New line depends on your OS:

DOS & Windows: \r\n 0D0A (hex), 13,10 (decimal)
Unix & Mac OS X: \n, 0A, 10
Macintosh (OS 9): \r, 0D, 13

More details here: https://ccrma.stanford.edu/~craig/utility/flip/

When in doubt, use any freeware hex viewer/editor to see how a file encodes its new line.

For me, I use following guide to help me remember: 0D0A = \r\n = CR,LF = carriage return, line feed

Sun
  • 2,487
  • 1
  • 21
  • 35
  • 8
    Strangely enough, I'm one of these people who knows `0D0A` by heart, but still confuses the `\r` and `\n`. – Nyerguds Mar 28 '13 at 12:41
  • 1
    Or, I remember \r is carriage return and \n is line feed... it's just I don't remember their order, so it doesn't help in remembering which one is 0D and which one is 0A ;) – Nyerguds Apr 20 '15 at 06:54
23

The sequence is CR (Carriage Return) - LF (Line Feed). Remember dot matrix printers? Exactly. So - the correct order is \r \n

Otávio Décio
  • 70,467
  • 17
  • 156
  • 220
  • I think this is the most logical answer – David Snabel-Caunt Jun 30 '11 at 19:20
  • 12
    LF+CR would do the same CR+LF on printers. – ikegami Jun 30 '11 at 19:25
  • 11
    I disagree, @David. Merely thinking of dot-matrix printers (or typewriters, for that matter, since *they're* the ones that have a carriage to return) isn't enough. With only basic information about how they work, there's no reason to think it matters whether we advance the paper before or after returning to the start of the line. Both \r\n and \n\r put the paper in the same position. – Rob Kennedy Jun 30 '11 at 19:29
  • @ikegami on _most_ printers, not on all of them. They also had DIP switches to configure that behavior. :) – Daniel Mošmondor Jan 31 '12 at 15:06
  • 5
    One reason for putting the line feed after the carriage return is that reduces the total amount of time required--while the head is returning to the left edge of the paper, the platen can index one line. The longer operation starts first, so the whole operation completes in the time it takes the longer operation to execute. We did a lot of things for reasons like that back then; imagine waiting out a couple of hundred lines at 110 baud... – Jerry Andrews Feb 22 '12 at 15:58
  • @JerryAndrews still it gives no many clues why CR first. If platen rolls while head is returning, it means multitasking. Then no clues why printer won't be moving head while rolling paper (if rolling started first). Even on 110 baud, it's about 1 second loss per 14 lines. – LogicDaemon Jan 20 '15 at 10:58
8

In any .NET langauge, Environment.NewLine would be preferable.

Esteban Araya
  • 27,658
  • 22
  • 99
  • 139
5

Be careful with doing this manually.
In fact I would advise not doing this at all.

In reality we are talking about the line termination sequence LTS that is specific to platform.

If you open a file in text mode (ie not binary) then the streams will convert the "\n" into the correct LTS for your platform. Then convert the LTS back to "\n" when you read the file.

As a result if you print "\r\n" to a windows file you will get the sequence "\r\r\n" in the physical file (have a look with a hex editor).

Of course this is real pain when it comes to transferring files between platforms.

Now if you are writing to a network stream then I would do this manually (as most network protocols call this out specifically). But I would make sure the stream is not doing any interpretation (so binary mode were appropriate).

Martin York
  • 234,851
  • 74
  • 306
  • 532
4

\r\n

Odd to say I remember it because it is the opposite of the typewriter I used.
Well if it was normal I had no need to remember it... :-)

typewriter from wikipedia *Image from Wikipedia

In the typewriter when you finish to digit the line you use the carriage return lever, that before makes roll the drum, the newline, and after allow you to manually operate the carriage return.

You can listen from this record from freesound.org the sound of the paper feeding in the beginning, and at around -1:03 seconds from the end, after the bell warning for the end of the line sound of the drum that rolls and after the one of the carriage return.

Hastur
  • 2,230
  • 24
  • 32
4

\r\n for Windows will do just fine.

Brad
  • 146,404
  • 44
  • 300
  • 476
  • A single `\n` does just fine on windows too, including the latest version of Notepad. – André Caron Jun 30 '11 at 19:08
  • I think modern editors will detect line endings, as different OS use different endings. – David Snabel-Caunt Jun 30 '11 at 19:23
  • 1
    Actually not: If you print this to a file (std C/C++ file opened in text mode) on windows the physical file will contain "\r\r\n" (check it with a hex editor). When you read it back the "\r\n" is converted back into a single "\n". – Martin York Jun 30 '11 at 19:42
  • 2
    Actually, @DavidCaunt, I just tried \n and it does not work with notepad on Win8. – philk Jan 24 '14 at 13:24
4

From Wikipedia (you can read which is correct for your OS at that article):

Systems based on ASCII or a compatible character set use either LF (Line feed, '\n', 0x0A, 10 in decimal) or CR (Carriage return, '\r', 0x0D, 13 in decimal) individually, or CR followed by LF (CR+LF, '\r\n', 0x0D0A).

bhamby
  • 14,376
  • 1
  • 40
  • 62
2

if you are using C#, why not using Environment.NewLine ? (i assume you use some file writer objects... just pass it the Environment.NewLine and it will handle the right terminators.

NirMH
  • 4,247
  • 2
  • 36
  • 59