Mateen Ulhaq
Mateen Ulhaq

Reputation: 27201

What is the point of using *both* Carriage Returns and Line Feeds?

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?

Upvotes: 4

Views: 1495

Answers (4)

David Heffernan
David Heffernan

Reputation: 612993

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.

Upvotes: 0

John Saunders
John Saunders

Reputation: 161783

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

Upvotes: 16

Greg Buehler
Greg Buehler

Reputation: 3894

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.

Upvotes: 1

viraptor
viraptor

Reputation: 34155

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.

Upvotes: 1

Related Questions