C++ 在C+中查找精确的字符串匹配+;

C++ 在C+中查找精确的字符串匹配+;,c++,string,match,C++,String,Match,下面是我用来检测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

下面是我用来检测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();
}
错误是,程序将不会查找“GetSpheresAndVol”,因为它在“GetSA”处停止。显然,程序认为“GetSphereSANDVOL”包含“GetSA”,因此它将执行:

if(found = line.find("GetSA"))
    std::cout<<"We found GetSA."<<std::endl;
if(find=line.find(“GetSA”))

std::cout您误解了
find
的工作原理。读这本书

条件应该是这样的:

if ((found = line.find("xyz")) != line.npos) { /* found "xyz" */ }
int main(int argc, char * argv[])
{
    if (argc != 2) { std::cout << "Bad invocation\n"; return 0; }

    std::ifstream infile(argv[1]);

    if (!infile) { std::cout << "Bad filename '" << argv[1] << "'\n"; return 0; }

    for (std::string line; std::getline(infile, line); )
    {
        int pos;

        if ((pos = line.find("abc")) != line.npos)
        {
            std::cout << "Found line 'abc'\n";
            continue;
        }

        if ((pos = line.find("xyz")) != line.npos)
        {
            std::cout << "Found line 'xyz'\n";
            continue;
        }

        // ...

        std::cout << "Line '" << line << "' did not match anything.\n";
    }
}

我会这样写你的整个程序:

if ((found = line.find("xyz")) != line.npos) { /* found "xyz" */ }
int main(int argc, char * argv[])
{
    if (argc != 2) { std::cout << "Bad invocation\n"; return 0; }

    std::ifstream infile(argv[1]);

    if (!infile) { std::cout << "Bad filename '" << argv[1] << "'\n"; return 0; }

    for (std::string line; std::getline(infile, line); )
    {
        int pos;

        if ((pos = line.find("abc")) != line.npos)
        {
            std::cout << "Found line 'abc'\n";
            continue;
        }

        if ((pos = line.find("xyz")) != line.npos)
        {
            std::cout << "Found line 'xyz'\n";
            continue;
        }

        // ...

        std::cout << "Line '" << line << "' did not match anything.\n";
    }
}
intmain(intargc,char*argv[])
{

如果(argc!=2){std::cout有两个错误,一个是您询问的,另一个是您没有询问的

您的if语句是错误的。您误解了
string::find
的工作原理。这是正确的方法

        if ((found = line.find("GetSA")) != string::npos)
           ...
        else if ((found = line.find("GetVol")) != string::npos)
           ...
        etc.
如果
string::find
没有找到它要查找的内容,它将返回一个特殊值
string::npos
。这是您的If条件应该测试的内容


第二个错误,在(!file.eof()时丢失
loop,这是完全不必要的。

string::find
函数返回
string::npos
,如果找不到,则返回一个索引。您假设它返回一个布尔值,并进行相应的测试。这将不起作用,因为
string::npos
的计算结果为布尔真值(非零)。此外,如果子字符串的索引为零,则不会通过

您必须这样做:

if( std::string::npos != (found = line.find("GetSA")) )
   // etc...
就我个人而言,我不喜欢以这种方式设置值和测试的风格,但这取决于您。我可以用一个简单的助手函数来代替:

bool FindSubString( std::string& str, const char *substr, int& pos )
{
    pos = str.find(substr);
    return pos != std::string::npos;
}
然后:

但在您的情况下,您甚至没有使用
found
变量。因此,您可以忽略我所说的有关样式的内容,只需执行以下操作:

if( std::string::npos != line.find("GetSA") )
    // etc...

外部while循环没有任何作用。
if( std::string::npos != line.find("GetSA") )
    // etc...