BartC said:
jacob navia said:
Le 04/05/2014 03:09, Keith Thompson a écrit :
lcc64 ctrlz.c // Compile it with a good compiler
lcclnk64 ctrlz.obj // Link it with a good linker
ctrlz.exe // Execute
saw_A = 1
saw_Z = 1
saw_Ctrl_Z = 1
[...]
Apart from MSVC which apparently gives 1,0,0, that is also the result with
gcc, PellesC, DMC, Clang and g++, all running on Windows.
gcc on Windows is often installed as part of a POSIX-like layer
that imposes its own semantics. There are serveral ports of gcc
to Windows, and I doubt that they all behave the same way. And of
course it's the runtime library, not the compiler, that's relevant;
some implementations (like MinGW, I think) combine gcc with the
MS runtime library, others combine gcc with some other library
(like Cygwin).
And g++ is not a C compiler, so ...
There are two major differences between POSIX-style and Windows-style
text files: the end-of-line representation and the treatment
of Ctrl-Z as an end-of-file marker. It would be interesting to
see, for each of the compilers you mention, how it treats both.
For Ctrl-Z handling, you can use the program I posted earlier.
For end-of-line representation, you can write '\n' to a text file
in text mode, then read it back in binary mode.
I'm not surprised that some implementations might use Windows-style
handling for end-of-line and POSIX-style handling of Ctrl-Z, nor
do I suggest that that approach is better or worse than any other.
(gcc under Linux gave 1,1,1.)
So lcc-win is the odd-one-out, in text mode.
(In binary mode, which I generally use, that gives 1,1,1 always.)
And either behavior is perfectly valid as far as the C standard is
concerned.
N1570 7.21.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 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.
My advice: Use text mode for text, binary mode for non-text. If you
care what happens when you write '\x1a' to a file and read it back
(assuming, as in most commonly used character sets, that '\x1a' is a
control character), then you're not working with text.
Admittedly it's not always that simple, especially if you have a
requirement to deal with "foreign" text files.