Victor said:
Default said:
Victor said:
James Kanze wrote:
[..writing '\0' to a text stream is implementation-defined..]
an
implementation can map the '\0' character to something else in a
text file, or even use some system API which treats the buffer
as a null terminated string. Text streams are for text, and
'\0' is not text.
Can you back this up with anything? Why is '\0' not text? I
cannot find any explicit definition of "text" in the Standard
that would say that '\0' is not text. It's not part of the
basic character set, but that has nothing to do with "not text",
or does it?
Fairer to say that it's not a printable character.
Yes, but does it mean that it cannot be output to a stream opened
in "text" mode?
I think it almost certainly can. I didn't really find much in the C++
standard on the topic, so I fell back to the C standard (C99 draft).
[#2] 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 one-to-one
correspondence between the characters in a stream and those
in the external representation. Data read in from a text
stream will necessarily compare equal to the data that were
earlier written out to that stream only if: the data consist |
only of printing characters and the control characters
horizontal tab and new-line; no new-line character is
immediately preceded by space characters; and the last
character is a new-line character. Whether space characters
that are written out immediately before a new-line character
appear when read in is implementation-defined.
So there seems to be no problem writing a character of any type to a
text stream, although conversion of some characters may take place
(CRLF of course). I think the contention of \0 perhaps being altered is
correct, but I don't think it's correct to call it "not text". Control
characters, of which \0 is one, can be written to text streams.
If we're talking about displays:
5.2.2 Character display semantics
[#1] The active position is that location on a display
device where the next character output by the fputc or
fputwc function would appear. The intent of writing a |
printing character (as defined by the isprint or iswprint
function) to a display device is to display a graphic
representation of that character at the active position and
then advance the active position to the next position on the
current line. The direction of writing is locale-specific.
If the active position is at the final position of a line
(if there is one), the behavior is unspecified.
This is followed by a discussion of several control characters and
their defined behavior, but \0 is not one of them. I'm hesitant to
declare that writing \0 to a display device is undefined behavior. I'd
have to take it to those more expert in reading the standard than I am.
Brian (standards diving on a Friday afternoon)