Sign in to follow this  

differences between \r\n and \n

This topic is 4398 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

This topic is 4398 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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