I am trying to understand the behaviour of newline characters when writing files in text mode on Windows (with MSVC). According to the Microsoft documentation:
it is stated that in text mode, LF
(line feed) characters are translated to CR LF
(carriage return + line feed) combinations on output.
Then, what happens if a thoughtful programmer tries to write the CR LF
combination in text mode on Windows, e.g.,
fprintf(stream, "\r\n");
Does this result in CR CR LF
, doubling the carriage return due to the translation, or is it handled differently (left as CR LR
)? Is this behaviour documented anywhere in detail?
As per the man page of fopen()
linked by Weather Vane in the comments:
In text mode, carriage return-line feed (CRLF) combinations are translated into single line feed (LF) characters on input, and LF characters are translated to CRLF combinations on output.
So:
In text mode, "\r\n"
outputs CR CR LF
.
In binary mode, "\r\n"
outputs, CR LF
.