txtファイルから行内の文字列を検出するために使用したコードは次のとおりです。
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();
}
そして、ここに私のログファイルがあります:
GetSA (3.000000)
GetVol (3.000000)
GetSphereSAandVol (3.000000)
GetVol (3.000000)
GetSphereSAandVol (3.000000)
GetSA (3.00000)
エラーは、プログラムが「GetSA」で停止するため、「GetSphereSAandVol」を見つけられないことです。明らかに、プログラムは「GetSphereSAandVol」に「GetSA」が含まれていると認識し、次のように実行します。
if(found = line.find("GetSA"))
std::cout<<"We found GetSA."<<std::endl;
プログラムが実行されることを期待しているので、これはまさに私が望むものではありません:
else if (found = line.find("GetSphereSAandVol"))
std::cout<<"We found GetSphereSAandVol."<<std::endl;
それで、とにかく私はこれを避けることができますか?本当に欲しいものを手に入れるには?どうもありがとう。