提问者:小点点

通过比较C++中的行在txt文件中进行搜索最简单的方法是什么?


我正在编写一个代码,从一个文本文件中从电影的名称搜索关于任何电影的信息。 关于那部电影的信息只有几行,我得把它们印在屏幕上。 我试着读其他东西,但看不懂。 而且我不擅长do/while循环,我更喜欢使用For循环。 我尝试使用find(),但无法使其工作。 我在这里完全是个初学者。 并且IF/ELSE部分中的部分不工作。 我不知道怎样才能比较台词。 另外,除了基本的东西,我不知道很多,所以很可能我不会知道你告诉我的任何事情。

bool search() // The search option to search in the txt file
{
    string choi;
    bool mainFlag = false;
    string line = "";

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

reSrch:

    string name = "n";
    int year = 0;
    string star = "n";

    string blunt;
    string blunt2;
    int blunt3;

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    cout << "\n Please enter the name of the Movie (or n to skip name) : ";
    cin >> blunt;
    if (blunt == "n")
        blunt = "n";
    else
        name = blunt;

    cout << "\n Please enter a name of any one star of the Movie (or n to skip name of star) : ";
    cin >> blunt2;
    if (blunt2 == "n")
        blunt2 = "n";
    else
        star = blunt2;

    cout << "\n Please enter the year of production of the Movie (or 0 to skip year) : ";
    cin >> blunt3;
    if (blunt3 == 0)
        blunt3 = 0;
    else
        year = blunt3;

    ////////////////////////////////////////////////////////////////////////////////////////////

    if (blunt == "n" && blunt2 == "n" && blunt3 == 0)
    {
        cout << "\n You have not entered any of the information to make a search. Would you\n like to try again? If not then you will be sent back to the main menu."
            "\n\n Please enter yes or no : ";
        cin >> choi;

        if (choi == "YES" || choi == "Yes" || choi == "yes")
        {
            Sleep(1500);
            system("CLS");
            goto reSrch;
        }
        else
        {
            Sleep(1500);
            system("CLS");
            //mainFlag = true;
        }
    }

    else
    {
        if (name != "n")
        {
            if (star != "n")
            {
                if (year != 0)
                {
                    mov.open("movie.txt");


                    for (unsigned int curLine = 0; getline(mov, line); curLine++) 
                    { 
                        if (line.find(name, 0) != string::npos)//npos is used to tell = no matches found
                        {
                            cout << "found: " << search << "line: " << curLine << endl;
                        }
                    }
                    for (unsigned int curLine = 0; getline(mov, line); curLine++)
                    {
                        if (line.find(star, 0) != string::npos)
                        {
                            cout << "found: " << search << "line: " << curLine << endl;
                        }
                    }
                    for (unsigned int curLine = 0; getline(mov, line); curLine++)
                    {
                        if (line.find(year, 0) != string::npos)
                        {
                            cout << "found: " << search << "line: " << curLine << endl;
                        }
                    }
                    mov.close();
                }
                else
                {
                    mov.open("movie.txt", ios::app);
                    for (unsigned int curLine = 0; getline(mov, line); curLine++)
                    {
                        if (line.find(name, 0) != string::npos)
                        {
                            cout << "found: " << search << "line: " << curLine << endl;
                        }
                    }
                    for (unsigned int curLine = 0; getline(mov, line); curLine++)
                    {
                        if (line.find(star, 0) != string::npos)
                        {
                            cout << "found: " << search << "line: " << curLine << endl;
                        }
                    }
                    mov.close();
                }
            }
            else
            {
                if (year != 0)
                {
                    mov.open("movie.txt", ios::app);
                    for (unsigned int curLine = 0; getline(mov, line); curLine++)
                    {
                        if (line.find(name, 0) != string::npos)
                        {
                            cout << "found: " << search << "line: " << curLine << endl;
                        }
                    }
                    for (unsigned int curLine = 0; getline(mov, line); curLine++)
                    {
                        if (line.find(year, 0) != string::npos)
                        {
                            cout << "found: " << search << "line: " << curLine << endl;
                        }
                    }
                    mov.close();
                }
                else
                {
                    mov.open("movie.txt", ios::app);
                    for (unsigned int curLine = 0; getline(mov, line); curLine++)
                    {
                        if (line.find(name, 0) != string::npos)
                        {
                            cout << "found: " << search << "line: " << curLine << endl;
                        }
                    }
                    mov.close();
                }
            }
        }
        else
        {
            if (star != "n")
            {
                if (year != 0)
                {
                    mov.open("movie.txt", ios::app);
                    for (unsigned int curLine = 0; getline(mov, line); curLine++)
                    {
                        if (line.find(star, 0) != string::npos)
                        {
                            cout << "found: " << search << "line: " << curLine << endl;
                        }
                    }
                    for (unsigned int curLine = 0; getline(mov, line); curLine++)
                    {
                        if (line.find(year, 0) != string::npos)
                        {
                            cout << "found: " << search << "line: " << curLine << endl;
                        }
                    }
                    mov.close();
                }
                else
                {
                    mov.open("movie.txt", ios::app);
                    for (unsigned int curLine = 0; getline(mov, line); curLine++)
                    {
                        if (line.find(star, 0) != string::npos)
                        {
                            cout << "found: " << search << "line: " << curLine << endl;
                        }
                    }
                    mov.close();
                }
            }
            else
            {
                if (year != 0)
                {
                    mov.open("movie.txt", ios::app);
                    for (unsigned int curLine = 0; getline(mov, line); curLine++)
                    {
                        if (line.find(year, 0) != string::npos)
                        {
                            cout << "found: " << search << "line: " << curLine << endl;
                        }
                    }
                    mov.close();
                }
            }
        }
    }


    return mainFlag;
}

共1个答案

匿名用户

你的逻辑错了。 您的代码假设您可以针对每种不同的情况多次读取文件,但事实并非如此。 您必须读取文件一次,然后在读取时对每一行应用适当的逻辑。

加上year是一个整数,您不能使用find()查找整数,您需要首先将整数转换为字符串,我使用to_string函数来实现这一点。

像这样的东西

 string year_string = to_string(year);
 for (unsigned int curLine = 0; getline(mov, line); curLine++) 
 {
       bool name_ok = name == "n" || line.find(name) != string::npos;
       bool star_ok = star == "n" || line.find(star) != string::npos;
       bool year_ok = year == 0 || line.find(year_string) != string::npos;
       if (name_ok && star_ok && year_ok)
       {
           cout << "found: " << search << "line: " << curLine << endl;
       }
 }

_ok变量为true,如果在该类别中没有给出要搜索的内容,或者如果给出了要搜索的内容并且找到了它。 如果所有_ok变量都为true,那么我们就找到了要查找的内容。

这段代码也是你曾经代码长度的1/10左右,这也是一件好事。

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(c++|中|行在|txt|文件|中|搜索|简单|方法)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?