First: please use “nullptr” instead of NULL. NULL is just a macro and means 0. Thus the error message claims it is comparing to long because it is interpreting NULL as number.
However, an object cannot be nullptr, only pointers can be nullptr!
Things to fix:
prefer std::string for reading (dont need to worry about line numbers)
you can loop directly over your list of numbers (always prefer the foreach loop with for( : ) instead of for( ; ; ))
open stream in constructor and keep scope minimal
at the end, you can check if you’ve read to eof. If not, an error has occurred while reading
Suggestion to rewrite (untested):
int allN[] = {4, 7, 10, 13, 16, 19, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60};
for (int n : allN) {
std::ifstream fp(Form("test_%d.txt", n));
if (fp.is_open()) {
std::ofstream fout(Form("out_test_%d.txt", n));
if (!fout.is_open()) std::cerr << "Cannot open fout\n";
std::string line;
while (std::getline(fp, line)) {
if (line[4] == '2' && line[6] == '4') {
fout << line << "\n";
// cout << line << endl; This line is for the checking that this function doing well.
}
}
// optionally add error checking, is fp at eof? is fout good?
fout.close(); if (! fout) std::cerr << "Error wrtinng\n";
if (!fp.eof()) std::cerr << "Error reading\n";
}
}