简体   繁体   中英

C++: I don't understand how this while loop works

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!

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 (ie, 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.

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

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."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM