Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 753
Mateen Ulhaq Avatar asked Dec 04 '22 09:12

Mateen Ulhaq


2 Answers

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

like image 133
John Saunders Avatar answered Apr 28 '23 00:04

John Saunders


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.

like image 45
viraptor Avatar answered Apr 28 '23 01:04

viraptor