Let's say I have a file that has
100 text
If I try reading 2 numbers using ifstream, it would fail because text
is not a number. Using fscanf I'll know it failed by checking its return code:
if (2 != fscanf(f, "%d %d", &a, &b))
printf("failed");
But when using iostream instead of stdio, how do I know it failed?
Its actually as (if not more) simple:
Get used to that format, by the way. as it comes in very handy (even more-so for continuing positive progression through loops).
If one' using GCC with
-std=c++11
or-std=c++14
she may encounter:Why? The C++11 standard made
bool
operator call explicit (ref). Thus it's necessary to use:Personally I prefer below use of
fail
function: