cin.readsome() successfully read 0 characters:
next char: 1
cin.readsome() successfully read 0 characters:
next char: 2
cin.readsome() successfully read 0 characters:
next char: 3
My queries:
1) readsome()'s functionality is highly implementation-dependent (according to documentation). Is this the way readsome() is supposed to work with cin or am I doing something wrong?
Evidently, synchronization of the standard C++ streams with the standard C streams (which is done by default) causes the standard C++ streams to be unbuffered. Why?
It would appear that synchronization and buffering are mutually exclusive. Is this so and why?
> It would appear that synchronization and buffering are mutually exclusive
For the standard streams, C specifies:
At program startup, three text streams are predefined and need not be opened explicitly — standard input (for reading conventional input), standard output (for writing conventional output), and standard error (for writing diagnostic output). As initially opened, the standard error stream is not fully buffered; the standard input and standard output streams are fully buffered if and only if the stream can be determined not to refer to an interactive device.
It may be possible to change the buffering of the standard C streams with std::setbuf / std::setvbuf; however, the C standard states that buffering behaviour is implementation defined. C99, re. buffering:
Support for these characteristics is implementation-defined, and may be affected via the setbuf and setvbuf functions.
Microsoft: http://rextester.com/ZZVC16680
Note that in the Microsoft library, even afterstd::setbuf, the buffering of stdin starts only after the actual first input.
So, from your reply, I understand the following 2 points:
1) It's basically because C specifies stdin, stdout and stderr to be unbuffered, that cin, cout and cerr are unbuffered, when synchronized with the former.
2) Otherwise, synchronization and buffering needn't be mutually exclusive.
Evidently, synchronization of the standard C++ streams with the standard C streams (which is done by default) causes the standard C++ streams to be unbuffered. Why?
It would appear that synchronization and buffering are mutually exclusive. Is this so and why?
Borges has affirmed this with his reply.
However, now look at what Stroustrup has to say ["CPL", 4th Ed, pp 1088-1089] :
A call of sync_with_stdio(true) ... guarantees that the iostream and stdio operations share buffers.
A call of sync_with_stdio(false) ... prevents buffer sharing ...
Thus, Stroustrup is saying that synchronization causes buffer sharing, not unbuffering.
> It's basically because C specifies stdin, stdout and stderr to be unbuffered
C specifies that stdin and stdout are fully buffered if and only if "the stream can be determined not to refer to an interactive device"; and that stderr is never fully buffered.
In almost every implementation, stdout (referring to an interactive device) is line-buffered.