bartc said:
Yes, carriage return. But why insert explicit carriage return into a text
stream that is supposed to full of converted "\n" characters? On my machine
carriage return is the code 13, which gives mixed results in your test
below.
I'll address the "why" below.
Four characters and two line endings:
* When displayed on the console, the first \r causes the "a" to be
overwritten, so shows "b"
* Inside notepad, shows "ab" but with an invisible extra character between a
and b
* Inside "edit" (an ancient Dos editor) it shows a little music symbol after
a and after b
* Inside "wordpad" and "word", a and b are each shown on their own
lines.
The definitive answer is how C sees it. If the output is read back
using fgets, how many characters (before the \n that fgets will
insert) are there and how many lines?
From Keith's reply, I accept that pretty much anything goes, but
common sense suggests that only a system that uses \r as an ending (on
its own) would choose to do anything special with it.
I've tried two implementations on Windows and they both read back 1
line with 4 characters before the \n. The file on disc, of course,
contains (in hex) 61 0d 62 0d 0d 0a.
In other words, using "\r" is asking for trouble, unless one wants to
perhaps generate a Mac text file on a machine that is not a Mac.
Yes, but the permission in the standard cuts both ways. It is
permissible for \r not to generate a line ending even on a legacy Mac
OS C implementation. \r could be mapped to some other (possible
useful) character because \n must be mapped to a Mac OS line ending.
I.e. my example might generate one line with 4 characters on it even
there. I accept this is unlikely -- as a practical matter, an old Mac
OS C implementation is probably going to assume \r is intended to end
a line even though this is a non-portable way to do so.
In fact, this would be a good example of where a binary stdout is useful, if
you want to output explicit control codes (for example, if stdout is sent to
a device where every control character is significant).
But like Rod said, an embedded \r is unlikely in normal Ascii text, if using
code 13, and not a problem if it isn't. I'd like to see a text file, perhaps
using LF for newlines, that had \r scattered through it. Then I'd like to
know Why...
C defines what it means in 5.2.2 p2: "Moves the active position to the
initial position of the current line". I have seen it used (and used
it myself) for this purpose; for example, to generate text that
overwrites previous text to display, say, a percentage progress. It
is far from ridiculous to use these few "standard" control characters
in text output despite it not being guaranteed to work on all C
implementations.
On a practical note, using \r in this way works on Unix-like and
DOS-like systems (that includes Windows) on the implementations that I
am aware of. It /could/ work even on an old Mac, but I accept that
Mac C IO is probably not written that way.
Perhaps a _newline string constant should be in a header somewhere,
containing whatever newline sequence was in effect. That would help people
do their own thing if they wanted. That is, for systems where newline isn't
something crazy, like an entry in a database the other side of the file
system.
The boat has already sailed on that one. C was standardised when
there was (a) a pressing need to fix this (the PC/workstation boom was
underway) and (b) record-oriented output was still around. OK, it
probably still is around, but it was far more common then.