I have good input files that look like this:
734 220 915 927 384 349 79 378 593 46 2 581 500 518 556 771 697
571 891 181 537 455
and bad input files that look like this:
819 135 915 927 384 349 79 378 593 46 2 581 500 518 556 771 697
551 425 815 978 626 207 931 ABCDEFG 358 16 875 936 899 885 195 565
571 891 181 537 110
where there is a space following the last integer at the end of both files. I'm trying to write a script in C++ that will read in all the integers unless there is a char/string as in the second example in which case it would alert me of this. I tried to write it like this:
int main()
{
int n;
bool badfile = false;
ifstream filein("data.txt");
while (!filein.eof())
{
filein >> n;
if(filein.fail())
{
cout << "Not an integer." << endl;
badfile = true;
break;
}
cout << n << " ";
}
cout << endl << "file check: " << badfile << endl;
}
but filein.fail()
is triggered by the space at the end of a good file as well as a char/string in a bad file. So how can I set this up so that it ignores white spaces? Why does it only fail if there's a space at the end instead of either failing at all spaces or ignoring them altogether?