首页 > 解决方案 > 通过比较 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;
}

标签: c++

解决方案


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

Plusyear是一个整数,你不能用它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类别中搜索任何内容,或者如果有某些内容进行搜索并且找到了,则变量为真。如果所有_ok变量都为真,那么我们就找到了正在寻找的东西。

这段代码也大约是你的代码长度的 1/10,这也是一件好事。


推荐阅读