-1

I'm trying to understand how the following while loop works:

//...
ifstream inf(filename);
char command;
int value;
//...
 while(inf >> command >> value)
  {
    if (command == 'i')
        list.insert(value,listItr);
        else
            list.remove(value);
  }

I read the answers to a similar question and understand how the while loop condition returns a bool. But I'm confused on how the command and value variables are assigned. The file being read looks like this:

Has a series of 250,000 insertions in order from 1 to 250000
i1 i2 i3 i4 i5 i6 i7 i8 i9 i10 i11 i12 i13 i14 i15 i16 i17 i18 i19 i20 i21 i22 i23 i24 i25...

I figured the while condition would succeed only when reading i1 i2 etc. However, the loop gets past the first line of the file. Does the condition return true as long as any characters are read, regardless if they were mapped to variables?

Community
  • 1
  • 1

2 Answers2

2

If you take a look at the stream operator>>:

istream& operator>> (bool& val);
^^^^^^^^
istream& operator>> (short& val);
^^^^^^^^
...

You'll see that all its versions return an istream&. So, once inf >> command has been executed, the resulting istream will be used to read data into value. This is kind of like equivalent to run inf >> command, and then inf >> value.

After that, the reason why you are able to run the loop is simple. The last read (inf >> value) does also return an istream&. And istream has the bool operator implemented. Therefore, you can "automatically" check if the istream still has data to be read.

Rubens
  • 14,478
  • 11
  • 63
  • 92
  • 1
    Excellent explanation. Still adjusting to C++ and its dense syntax. Sometimes I'm lost on what to look for in the documentation. Thanks! – user1070725 Jun 29 '15 at 00:46
0

the loop gets past the first line of the file. Does the condition return true as long as any characters are read, regardless if they were mapped to variables?

No - you'll get false as soon as the parsing to variables fails. The inf >> command >> value code can set inf into a fail state, after which the conversion to boolean will cause the while loop to terminate. fail indicates inability to parse the input characters to form a value of the requested type.

By default, istream and its derivatives skip whitespace when using >>, so the >> command aspect requests the next non-whitespace character: any such character will be accepted. >> value is different though: it will only succeed if the value encountered can be parsed as a number: it may optionally start with - or +, it must have at least one digit ('0'-'9'), but not so many that it's out of range for the destination type.

If the input contains something like say i1E6, that would be parsed as command i, value 1 followed by command E, value6: i.e. theE` doesn't introduce a power-of-ten exponent the way it does for floating point numbers.

Only errors in which there's not even one numeric digit at the point value is expected will be reported. You could improve on this by rejecting values of command other than i and say e, r or d for erase, remove or delete....

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252