Here is the code I used to detect the string in a line from a txt file:
int main()
{
std::ifstream file( "C:\\log.txt" );
std::string line;
while(!file.eof())
{
while( std::getline( file, line ) )
{
int found = -1;
if((found = line.find("GetSA"))>-1)
std::cout<<"We found GetSA."<<std::endl;
else if ((found = line.find("GetVol"))>-1)
std::cout<<"We found GetVol."<<std::endl;
else if ((found = line.find("GetSphereSAandVol"))>-1)
std::cout<<"We found GetSphereSAandVol."<<std::endl;
else
std::cout<<"We found nothing!"<<std::endl;
}
}
std::cin.get();
}
And here is my log file:
GetSA (3.000000)
GetVol (3.000000)
GetSphereSAandVol (3.000000)
GetVol (3.000000)
GetSphereSAandVol (3.000000)
GetSA (3.00000)
The error is, the program will not go to find “GetSphereSAandVol”, because it stops at “GetSA”. Obviously, the program thinks “GetSphereSAandVol” contains “GetSA”, so it will execute:
if(found = line.find("GetSA"))
std::cout<<"We found GetSA."<<std::endl;
which is not exactly what I want, because I am expecting the program to execute:
else if (found = line.find("GetSphereSAandVol"))
std::cout<<"We found GetSphereSAandVol."<<std::endl;
So, anyway I can avoid this? to get what I really want? Thanks a lot.
You misunderstand how
findworks. Read the documentation.The conditionals should go like this:
I would write your entire program like this: