3

I'm still confused about what exactly the C++17 standard says about what is allowed to be read in from and written to standard in and out.

Does a program invoke undefined behavior if it accepts arbitrary binary data via standard in or outputs arbitrary binary data to standard out (even though many programs do that in practice)?

FreeLang
  • 47
  • 3
  • 2
    These are lots of questions, rather than just one. All good questions to ask individually. – Cubic Nov 16 '18 at 16:49
  • The drafts of the C++ standards are [readily available to read](https://github.com/cplusplus/draft). As is [this pretty good reference site](https://en.cppreference.com/w/cpp). – Some programmer dude Nov 16 '18 at 16:50
  • I apologize if it's a lot of questions, I felt that they are interconnected and better asked as one question. I can ask them individually though if that is what is preferred. – FreeLang Nov 16 '18 at 16:53
  • @FreeLang SO appreciates 'doing your home work'. Your question would be better received if you first read the references yourself, and than ask specific, pointed questions which aim to resolve something which was not clear to you. As it stands, the only real response here is a copy-paste of large sections of C++ standard. – SergeyA Nov 16 '18 at 16:55
  • Also, please be specific about the language standard you are interested in. I doubt that anyone would do the work for you to consider all standards when answering such a question. – Swordfish Nov 16 '18 at 16:56
  • I edited the question so that it's more focused on one issue. Please let me know if this is still to be improved. – FreeLang Nov 16 '18 at 17:25

2 Answers2

2

stdio is completely neutral regarding the data transported over it.

If it weren't, it would break things like I/O redirection. If you couldn't send raw binary data over stdio you couldn't use insert tools like a gzip compressor into a shell pipe.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • 2
    C++ implementations are free to transform characters when stdio is opened in text mode (which is the default). I'm not aware of any standard way to reopen them in binary mode. – FreeLang Nov 16 '18 at 20:09
  • 4
    Not on Windows. Files opened in text mode get `\r\n` translated into `\n` and vice versa. –  Nov 16 '18 at 20:26
  • That's because with posix threads there is no difference between binary reading and text reading for filehandles. – L.C. Nov 16 '18 at 20:26
  • 1
    @FreeLang: You can use `_setmode` to change the translation mode, `_setmode(_fileno(stdin), _O_BINARY);` does the trick. – datenwolf Nov 17 '18 at 11:05
  • 3
    @datenwolf This is a non-standard hack. I appreciate the effort but this question was about what the **C++ standard** says about accepting and outputting binary data and whether that's undefined behavior, not about what some particular implementation offers. – FreeLang Nov 17 '18 at 11:57
1

The question is about the standard, so I think the answer given to this question applies to yours too:

The standard is not always as coherent as we would like, since it is a very large document, written (in practice) by a number of different people, and despite all of the proof-reading that does occur.

As the same answer suggests, it's hard to say what is ill-formed.

My understanding is that the standard is simply not explicit about that, but there are other language features (as demonstrated by example by datenwolf) that imply it is not ill-formed, whatever it means:

the simple fact that STDIN and STDOUT are file handles means they can be opened and read/written in binary mode.

L.C.
  • 1,098
  • 10
  • 21