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).
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
\r\n
for Windows will do just fine.
In any .NET langauge, Environment.NewLine
would be preferable.
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.
The sequence is CR (Carriage Return) - LF (Line Feed). Remember dot matrix printers? Exactly. So - the correct order is \r \n
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.
\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... :-)
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.
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).
Source: Stackoverflow.com