Question

I'd have thought one was enough. But what's the point of doing CRLF (0x0D0A), when you can simply use CR (0D)? Normally, whenever I'm using strings (C++), I do this:

myString = "Test\nThis should be a new line!\nAnother linefeed.";

NOTE: For non-C++ programmers reading this, "\n" is a linefeed (0x0A).

But should I really be doing this:

myString = "Test\r\nThis should be a new line!\r\nAnother carriage return/linefeed pair.";

NOTE: "\r" means carriage return (0x0D).


EDIT: Should this be on Programmers.SE?

Was it helpful?

Solution

Remember that these codes all came from old Teletype machines. These were effectively typewriters: it was necessary both to advance the paper by a line (line-feed), but also to return the print head (on the carriage) to the left side of the paper (carriage-return).

OTHER TIPS

Windows / Unix / old Mac systems have each different way of writing new lines in text files (not binary ones). If you're programming under windows, then in binary mode, you will read (and you probably want to write) CRLF endings. Under unix-like systems it would be just LF.

If you deal with your own data formats... it shouldn't really matter which way you choose. It all really depends only on what you want to do with the string and where did you get it from.

Some systems like UNIX and OSX just use linefeed, DOS used an additional carriage return in order to be compatible with teletype machines and Windows inherited the architecture.

You use both on Windows because that's the custom on Windows. It's that simple. But you only write both for files destined for Windows.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top