Thursday 2 March 2017

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



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 ?


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

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...