I have a program which reads integers from a text file and skips non-integers and strange symbols. Then text file looks like:
# Matrix A // this line should be skipped because it contains # symbol
1 1 2
1 1$ 2.1 // this line should be skipped because it contains 2.1 and $
3 4 5
I have to print out the matrix without strange symbols and non-integers line. That is the output should be:
1 1 2
3 4 5
My code
ifstream matrixAFile("a.txt", ios::in); // open file a.txt
if (!matrixAFile)
{
cerr << "Error: File could not be opened !!!" << endl;
exit(1);
}
int i, j, k;
while (matrixAFile >> i >> j >> k)
{
cout << i << ' ' << j << ' ' << k;
cout << endl;
}
But it fails when it gets the first # symbol. Anyone helps please ?