I'm a beginner in programming and I need help with this snippet of code presented in my book.
for (My_type var; ist >> var;) { // read until end of file // maybe check that var is valid // do something with var } if (ist.fail()) { ist.clear(); char ch; // the error function is created into the book : if (!(ist >> ch && ch == '|')) error("Bad termination of input\n"); } // carry on : we found end of file or terminator This example is about reading values from a file. I've tried to use this but I'm having some troubles in understanding how it works :
If I try to test the stream state after the loop I get both
eofandfailstate, how is that possibile ? How to avoid triggering bothfailandeof?When is EOF triggered exactly ? From my test it seems to be triggered when I reach the last value of a sequence, is this definition correct ?
Thank you.