Thursday 14 December 2017

How does reading file with while loops work in C++?

itemprop="text">


Why is it possible to read
a file using a while loop such
as



while (file >>
variable)


Or



while
(getline(xx,
yy))



Do the
>> and getline functions return
boolean values ?


itemprop="text">
class="normal">Answer



The stream
operators evaluate to a reference to the stream itself. This allows
chaining e.g. file >> variable >> variable
>> variable
.



When you combine
it with the fact that the stream object is convertible to boolean (whose value is
true iff no error flags are set) and, yes, you get that
effect.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...