0

I was looking at some questions on here that shows you how to read from a txt file, and then assign anything in it to separate variables. The answer used a while loop that looked like this...

ifstream file(“file.txt”);

int var1;
int var2;
int var3;

while (file >> var1 >> var2 >> var3)
{
    /* do something with name, var1 etc. */
    cout << var1 << var2 << var3 << “\n”;
}

My question is, what is happening in that while loop that allows it assign those variables the right value? I know that it works, because I used it, but I just don't understand how it works.

Here's the link I was looking at, just in case anybody wanted to also look at it. C++: Read from text file and separate into variable

Thanks!

Community
  • 1
  • 1
ThatBoiJo
  • 199
  • 1
  • 13
  • 2
    I think you can get some clarification [here](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – πάντα ῥεῖ Jun 24 '16 at 12:38
  • Are you asking about end-of-file detection, or the "operator chaining"? using `< – Roddy Jun 24 '16 at 12:40

3 Answers3

3

Basically, the "expression"(a) file >> var1 >> var2 >> var3 will attempt to read three values from the file stream and place them into the specified variables. It's basically no different than cin >> myVar on it's own (except that it's chanining the input to multiple variables).

The value of that entire expression will be true if and only if all three values are read successfully.

In that case, the loop body will execute, the three values will be printed out, and the loop will go back to the top and try to get the next three values.

At the point where the expression evaluates as false (i.e., three values are not read), the loop will stop.


(a) The actual pathway from someStream >> someVariable to a Boolean value involves a few steps within the stream class (the return value from operator>> being converted to a Boolean with operator bool) but the above explanation should be good enough for most purposes.

paxdiablo
  • 772,407
  • 210
  • 1,477
  • 1,841
  • 1
    That's not exactly the "value of the expression". When you write `while (file >> var)`, that gets parsed as `while(operator>>(file, var))`. Now `operator>>(ifstream&, int)` returns a `ifstream &`, which has an `explicit operator bool()` that allows it to be testable in a `while` loop guard (contextual conversion to bool). – peppe Jun 24 '16 at 12:43
  • @peppe, it's an expression in the context of the `while` statement, I'm not sure we need to go into that level of detail for what is basically a near-beginner question. I'll clarify but without too much added complexity. – paxdiablo Jun 24 '16 at 12:51
1

ifstream overloads operator>>. In your particular case it is:

basic_istream& operator>>( int& value );

as you see it returns reference to itself so you can rewrite:

 while (file >> var1 >> var2 >> var3)

as a function calls:

while (file.operator>>(var1).operator>>(var2).operator>>(var3))

additionally ifstream explicitly converts to bool which gives true if stream is ok, otherwise false. This conversion is done using operator bool, which is marked as explicit (C++11 and up). This specifier applied to conversion operator allows to use ifstream instance in while loop or if statements, but it will not allow you to for example assign it to bool variable:

bool b = file; // results in error
marcinj
  • 44,446
  • 9
  • 70
  • 91
1

The compiler will translate: file >> var1 >> var2 >> var3 as: file.operator>>(var1).operator>>(var2).operator(var3)

The return of basic_istream::operator>>(int&) is basic_istream& hence the extraction operator can be called repeatedly on it's return because it returns a reference to itself.

When you evaluate this as in: while(file >> var1 >> var2 >> var3) the compiler is really doing: while(file.operator>>(var1).operator>>(var2).operator(var3).operator bool()) And we can see that basic_stream provides operator bool() So that will be what is evaluated as the condition for the while loop. And operator bool():

Returns true if the stream has no errors and is ready for I/O operations. Specifically, returns !fail().

So the human readable interpretation of this line is: "Insert var1, var2, and var3 into file. If the insertions didn't fail continue the loop."

Jonathan Mee
  • 35,107
  • 16
  • 95
  • 241