Sign in to follow this  
Vexorian

differences between \r\n and \n

Recommended Posts

Vexorian    152
I eventually had to learn this, I knew it! , now that I am making a font thing for my game I finally figure out that I have to learn the difference between \r\n and \n

Share this post


Link to post
Share on other sites
SiCrane    11839
\r\n is a carriage return followed by a newline. \n is just the newline. Since the days of teletype machines is mostly gone, \r\n is usually converted to just \n and handled that way.

Share this post


Link to post
Share on other sites
JohnBolton    1372
Some operating systems use the value 0x0a ('\n') to mark the end of a line. Some use the value 0x0d ('\r') to mark the end of a line. Some use the sequence 0x0d,0x0a ("\r\n").

The C and C++ libraries automatically convert to the proper marker when a '\n' is written to file or stream in text mode, and convert from the end-of-line marker to '\n' when reading from a file or stream in text mode.

Share this post


Link to post
Share on other sites
Skeleton_V@T    512
For maximal compatibility you might want to use "\r\n" whenever possible. Some programs such as notepad don't interpret "\n" as "\r\n" hence don't display the text correctly.

Share this post


Link to post
Share on other sites
SiCrane    11839
Quote:
Original post by Skeleton_V@T
For maximal compatibility you might want to use "\r\n" whenever possible. Some programs such as notepad don't interpret "\n" as "\r\n" hence don't display the text correctly.


That's not really true; as long as you use the correct text mode flags on file output, the OS should convert \n to the correct line terminator for the operating system.

Share this post


Link to post
Share on other sites
Spoonbender    1258
Quote:
Original post by Skeleton_V@T
For maximal compatibility you might want to use "\r\n" whenever possible. Some programs such as notepad don't interpret "\n" as "\r\n" hence don't display the text correctly.


On the other hand, some programs don't expect the '\r' before \n, and so, they end up printing something like ^M at the end of every line.

Share this post


Link to post
Share on other sites
njpaul    367
I know when I did some .NET programming last week and used the text edit box I had to use \r\n to get a new line. Just using \n resulted in garbage.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote:
Original post by njpaul
I know when I did some .NET programming last week and used the text edit box I had to use \r\n to get a new line. Just using \n resulted in garbage.


.Net has Environment.NewLine to solve such problems

Share this post


Link to post
Share on other sites
dalleboy    324
Quote:
Original post by Anonymous Poster
Quote:
Original post by njpaul
I know when I did some .NET programming last week and used the text edit box I had to use \r\n to get a new line. Just using \n resulted in garbage.

.Net has Environment.NewLine to solve such problems

OT: But it really _should_ be transparent in WinForms controls, too sad it isn't. Getting/setting text for a control should do the same thing as opening a file in text-mode: writing \n should be converted to \r\n and reading \r\n should be converted to \n.

Share this post


Link to post
Share on other sites
me22    212
Windows: \r\n
Linux: \n ( although mostly ok with \r\n )
Mac: was \r, OS X is probably \n ( although it was always very good and lenient )

Note that some protocols require a specific one -- HTTP needs \r\n, iirc.

Share this post


Link to post
Share on other sites
helix    301
I've ran into this fairly often. For example, when coding a string splitter (from file i/o), splitting on a '\n' was insufficient. I had to make the delimeter tokens '\r\n'. I will next time have to research the flags I used. I suspect it was because I was using the old school file i/o stuff instead of streams.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this