On 2/21/2018 7:50 PM, James R. Kuyper wrote:
> On 02/21/2018 12:44 PM, Barry Schwarz wrote:
>> On Wed, 21 Feb 2018 12:05:57 -0500, "James R. Kuyper"
<snip>
>>> Could someone explain to me why the C++ version apparently read one more
>>> byte than the C version, which is also one more byte than the file size?
>>> Also, why infile.eof() was false at the end?
From Bjarne's book, about unformatted input:
"If you have a choice, use formatted input instead of these low-level
input functions." - the low-level input functions mentioned here include
istream::read()
>>
>> The description for readsome at
cplusplus.com says that it will stop
>> reading when there is no data in the stream buffer, even if end of
>> file has not been reached. That may answer you second question.
>
> I read something like that too, but it made readsome() seem rather
> useless, unless you're getting excessively intimate with the details of
> how the stream buffer gets filled, so I assumed that it was either a
> mistake on their part, or a misunderstanding of what they were saying on
> my part.
From the same Bjarne's book, following page:
"The following functions depend on the detailed interaction between the
stream buffer and the real data source and should only be used if
necessary and then very carefully" - the functions mentioned here
include istream::readsome()
>
> Still, that's probably the explanation for what I found (see below).
> Reviewing the description of readsome() with that in mind, it means that
> in_avail() has a different meaning than I thought it did.
>
In my experience, the iostream library was historically one of the most
controversial among C++ standard libraries (as opposed to STL which has
been doing great since the beginning). C++11 improved a lot on this, but
still for unformatted (a.k.a. binary) IO, the good old fread()/fwrite()
are hard to beat in terms of code cleanliness and robustness, meaning
istream::read() probably reaches a comparable level, but not much more,
IMHO.
Apart of traps like that indicated by Öö Tiib, I think the preference
for istream::read() would be more due to uniformity with the rest of the
code.