Why is CR LF changed to LF on Windows?

On Windows, when you read \r\n characters from a file (or stdin) in text mode, \r is split and you only read \n .

Is there a standard according to which it should be like that?

Can I be sure that this will be true for any compiler on Windows? Will there be other character combinations for the platform that will be replaced with \n on these platforms too?

I use this code to generate input and use this code to read it. The results are here . You may note some missing \r '

+7
source share
1 answer

Yes, this is due to compatibility with C. In C text streams, strings end with a newline. This is an internal representation of the text stream, as can be seen from the program. The I / O library transforms the internal representation and the external.

The internal representation is platform independent, while for the text there are various conventions for a specific platform. That the text mode point in the stream library; portable programs for word processing can be written that should not contain a bunch of #ifdef directives for working on different platforms or create your own platform-independent text abstraction.

It so happened that the internal representation for C text streams corresponds to the native representation of Unix text files, since the C language and its library originated in Unix. To port C programs to other platforms, a text stream abstraction has been added that makes text files on a non-Unix system look like Unix text files.

In the standard ISO / IEC 9899: 1999 ("C99") we have the following:

7.19.2 Threads

[...]

A text stream is an ordered sequence of characters consisting of lines, each line consisting of zero or more characters plus the terminating character of a new line. Whether the last line requires a trailing newline character defined by the implementation. Characters can be added, changed, or deleted at the input and output to match various conventions for representing text in a host environment. Thus, there should not be a one-to-one correspondence between the characters in the stream and the external representation.

My bold accent. C ++ streams are defined in terms of C streams. The C ++ standard does not describe text or binary mode, except for a table that maps various combinations of stream flags to strings suitable as mode arguments for fopen .

+15
source

All Articles