I want to read files from a txt file and compare some lines with regex. The first line of the txt file should start with the string #FIRST. And if the string should start with a '#' the line should be ignored and it should continue. So counter should have the value 1 which it does and it should go to the second if statement if(counter==1). However it doesn't go to the second if statement.
txt file:
#FIRST
#
#haha
I expect the output to be good\ngood after the code is run once.
The output is:
good.
And it should be
good.
good.
.........
#include <iostream>
#include <string>
#include <vector>
#include <regex>
#include <fstream>
#include <sstream>
int main() {
std::ifstream input("test.txt");
std::regex e("#FIRST");
std::regex b("haha");
int counter;
for (counter = 0; !input.eof(); counter++) {
std::cout << counter << "\n";
std::string line;
if (counter == 0) {
getline(input, line);
if (std::regex_match(line, e)) {
std::cout << "good." << std::endl;
counter++;
} else
std::cout << "bad." << std::endl;
break;
}
getline(input, line);
if (line[0] == '#')
continue;
if (counter == 1) {
getline(input, line);
if (std::regex_match(line, b)) {
std::cout << "good." << std::endl;
} else
std::cout << "bad." << std::endl;
break;
}
}
return 0;
}