Bartc said:
I wonder how much trouble it would have been for these committees to have
simply allowed "t" as an option? (Especially as this was a microsoft
extension).
It wouldn't have been much trouble at all. But it's even less trouble
to omit the 't' when you write a call to fopen().
Then code would have been self-documenting for those not familiar
enough with '4.9.5.3 of C89 or 7.19.5.3(3) of C99' to know whether
the default mode is text or binary.
I don't think a whole lot of effort should be expended in making C
legible to readers who don't know the language.
(Just a question about text mode -- which I never use so I'm not too
bothered -- does it translate only the expected newline characters of the
host system to \n or will it cope with mixed files?
So if a program was opening text files from Windows, Linux and Mac, say,
would it translate newline characters from all of those successfully into
\n? Ie. just \n and no superfluous \r characters.
If it does, how does it do it? If not (and some brief tests suggest so),
then what actual use is a text mode which doesn't work?)
Text mode typically deals correctly with text files in the format used
by the implementation. Translating foreign files from one text format
to another is beyond the scope of stdio. (You can certainly do it by
*using* stdio, but the conversions aren't built in.)
In fact, as far as the standard is concerned, *anything* might have to
be re-mapped in text mode; there's nothing special about the new-line
character.
C99 7.19.2p2:
A text stream is an ordered sequence of characters composed into
lines, each line consisting of zero or more characters
plus a terminating new-line character. Whether the
last line requires a terminating new-line character is
implementation-defined. Characters may have to be added,
altered, or deleted on input and output to conform to differing
conventions for representing text in the host environment. Thus,
there need not be a oneto- one correspondence between the
characters in a stream and those in the external representation.
Read the rest of 7.19.2 as well.
On most systems you're likely to encounter, mapping between '\n' and
whatever sequence the implementation uses to mark end-of-line is
probably the only non-trivial mapping that's done in binary mode. But
it's theoretically possible that attempting to read a text file in
binary mode won't give you anything sensible; it might not even be
possible to open it.
(Just a question about text mode -- which I never use so I'm not too
bothered -- does it translate only the expected newline characters
of the host system to \n or will it cope with mixed files?
stdio provides a sophisticated mechanism for dealing with whatever
text file format your system happens to provide, so you don't have to
know how text files are represented. Why would you not take advantage
of that?