Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #89489

Re: why additional \r before each \r\n

From James Kuyper <jameskuyper@alumni.caltech.edu>
Newsgroups comp.lang.c++
Subject Re: why additional \r before each \r\n
Date 2023-03-06 14:30 -0500
Organization A noiseless patient Spider
Message-ID <tu5f09$1a99$1@dont-email.me> (permalink)
References <tu532n$33e16$1@portraits.wsisiz.edu.pl>

Show all headers | View raw


On 3/6/23 11:06, Jivanmukta wrote:
> Please explain me why this code outputs text with \r\r\n, not \r\n. I
> run it in Windows 10. When I call my program with >__test.txt I see
> \r\r\n in file __text.txt.
> I don't know why additional \r in text outputed from get_help() - I
> have single END_LINE after each line (in returned string).
>
> #define END_LINE "\r\n"
>
> void failure_dirtyphp(string errors) {
> if (last_obfuscation_stream.is_open()) {
> OUTPUT(errors);
> last_obfuscation_stream.close();
> } else {
> cout << errors << endl;
> }
> unlink((wstr2str(tmp_dir()) + ::pid_filename).c_str());
> exit(EXIT_FAILURE);
> }
>
> main() {
> ...
> failure_dirtyphp(dirtyphp.get_usage() + END_LINE + dirtyphp.get_help()
> + END_LINE);

If you have a stream opened in text mode, when you send a '\n' to that
stream, it automatically gets converted to what method the standard
library uses to indicate the end of a line, which usually matches the
line-ending convention on that platform. The inverse conversion gets
performed when you read from a text file. As a result, you don't need to
write different code for platforms with different ways of indicating the
end of a line. See <https://en.m.wikipedia.org/wiki/Newline> for a list
of the wide variety of different methods used to represent the end of a
line in many different contexts. In particular, to break some
preconceptions you might have, take a look the descriptions for
record-based and fixed line length systems.

The C++ standard says almost nothing about text mode vs binary mode,
cross-referencing the C standard library for definitions of how the C++
I/O library routines work. Here's what the C standard says about it:

(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.

Note, in particular, the peculiar connection between new-lines and
spaces. That's intended to allow implementations which use fixed line
length systems, with end-of-line indicated by padding to the end with
blanks.

(7.21p3)
> Whether a write on a text stream causes
> the associated file to be truncated beyond that point is
> implementation-defined.



Back to comp.lang.c++ | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

why additional \r before each \r\n Jivanmukta <jivanmukta@poczta.onet.pl> - 2023-03-06 17:06 +0100
  Re: why additional \r before each \r\n Bo Persson <bo@bo-persson.se> - 2023-03-06 17:14 +0100
    Re: why additional \r before each \r\n Bo Persson <bo@bo-persson.se> - 2023-03-06 17:17 +0100
    Re: why additional \r before each \r\n Jivanmukta <jivanmukta@poczta.onet.pl> - 2023-03-06 18:55 +0100
      Re: why additional \r before each \r\n Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-03-06 10:00 -0800
        Re: why additional \r before each \r\n Jivanmukta <jivanmukta@poczta.onet.pl> - 2023-03-06 19:35 +0100
          Re: why additional \r before each \r\n Paavo Helde <eesnimi@osa.pri.ee> - 2023-03-07 08:34 +0200
            Re: why additional \r before each \r\n Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-03-07 12:17 -0800
  Re: why additional \r before each \r\n Jivanmukta <jivanmukta@poczta.onet.pl> - 2023-03-06 17:24 +0100
  Re: why additional \r before each \r\n James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-03-06 14:30 -0500

csiph-web