开发者

iostream, some questions

I've seen people do things like....

 istringstream ibuf;

 if (ibuf >>  zork >> iA >> Comma >> iB)

now I guess the value depends on what >>iB exposes bu开发者_开发百科t exactly what is that and what does it mean? Does true mean all the ietms were extracted?

Also, after an expression like

 ibuf >>  zork >> iA >> Comma >> iB;

is there any way to find out how many characters and items where extracted?


This works because of two properties of istream objects:

  • istreams return themselves after each extraction (the >> operator) to allow chaining multiple extractions (a >> b >> c)
  • istreams return their status (as though .good() were called) when they're cast/converted to bool, via overloading bool operator !()

Basically the code you wrote is a short-hand version of:

if ( ((((ibuf >> zork) >> ia) >> Comma) >> ib).good() ) {

}

Once all the extractions have occured, you're left with if (ibuf) which is implicitly like writing if ((bool)ibuf), which checks ibuf.good().

There is no way to get the number of characters extracted over a series of chained extractions, but you can find the number of characters extracted during the last operation with the function gcount. However, it only works for certain format-ignoring functions like get and getline, and not the extraction operator.


The second piece of code reads a set of values from the ibuf to the variables following it. However, the return of the hidden operator >>() call is an istringstream object. There's no direct way to get the character count.

You can check out the gcount member function which gives the number of characters for the last unformatted input operation. Note that this is per operation, so cascading can't be used. You can also use the read member function.

Edit:

(ibuf >>  zork >> iA >> Comma >> iB)

is actually:

((((ibuf.operator >>(zork)).operator >>(iA)).operator >>(Comma)).operator >>(iB))

The nesting level of parenthesis tell you the order of calls (and the arguments).


if (ibuf >>  zork >> iA >> Comma >> iB)

is logically equal to:

ibuf >> zork;
ibuf >> iA;
ibuf >> Comma;
ibuf >> iB;
if (ibuf) ...

"is there any way to find out how many characters and items where extracted?"

There is member function "gcount": http://www.cplusplus.com/reference/iostream/istream/gcount/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜