Reminder - \r\n or \n\r?

.NetWindowsAscii

.Net Problem Overview


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

.Net Solutions


Solution 1 - .Net

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

Solution 2 - .Net

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.

Solution 3 - .Net

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

Solution 4 - .Net

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

Solution 5 - .Net

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

Solution 6 - .Net

\r\n for Windows will do just fine.

Solution 7 - .Net

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).

Solution 8 - .Net

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).

Solution 9 - .Net

\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.

Solution 10 - .Net

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaniel MošmondorView Question on Stackoverflow
Solution 1 - .NetDavid Snabel-CauntView Answer on Stackoverflow
Solution 2 - .NetBrunoLMView Answer on Stackoverflow
Solution 3 - .NetSunView Answer on Stackoverflow
Solution 4 - .NetOtávio DécioView Answer on Stackoverflow
Solution 5 - .NetEsteban ArayaView Answer on Stackoverflow
Solution 6 - .NetBradView Answer on Stackoverflow
Solution 7 - .NetMartin YorkView Answer on Stackoverflow
Solution 8 - .NetbhambyView Answer on Stackoverflow
Solution 9 - .NetHasturView Answer on Stackoverflow
Solution 10 - .NetNirMHView Answer on Stackoverflow