I am trying to read a file line by line and compare it to string in the code. But somehow the following code is not giving expected result. I do not follow what am I missing during the comparison:
CODE
int main(int argc, char** argv)
{
std::string filePath="E:\\data\\stopfile.txt";
std::string line;
std::ifstream myfile;
std::string test="ball";
myfile.open(filePath.c_str());
if(myfile.is_open()){
while(getline(myfile,line)){
std::cout<<line<<std::endl;
if(!line.compare(test)){
std::cout<<"SUCCESS"<<std::endl;
}
else{
std::cout<<"FAIL"<<std::endl;
}
}
}
myfile.close();
if(!test.compare("ball")){
std::cout<<"SUCCESS"<<std::endl;
}
}
OUTPUT
apple
FAIL
ball
FAIL
cat
FAIL
SUCCESS
I expect the program to print SUCCESS after the line of "ball". But the comparison does not seem to be success.
I have also tried the comparison condition to
if(!line.compare(test.c_str())){
Still the result is the same.