当前位置: 首页 > 知识库问答 >
问题:

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

万俟嘉珍
2023-03-14

我正在编写一个代码,从一个文本文件中从电影的名称搜索关于任何电影的信息。 关于那部电影的信息只有几行,我得把它们印在屏幕上。 我试着读其他东西,但看不懂。 而且我不擅长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个答案

任飞龙
2023-03-14

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

加上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左右,这也是一件好事。

 类似资料:
  • 问题内容: 我正在为生成Excel文件(二进制文件)的某些代码编写JUnit测试。我还有另一个包含预期输出的Excel文件。将实际文件与预期文件进行比较的最简单方法是什么? 当然,我可以自己编写代码,但是我想知道在受信任的第三方库(例如Spring或Apache Commons)中是否已有这样做的方法。 问题答案: 这就是我最终要做的(由DBUnit完成繁重的工作): 这将比较两个文件中的数据,没

  • 我尝试在给定的文本文件中连接两行文本,并将输出打印到控制台。我的代码非常复杂,有没有更简单的方法通过使用文件处理基本概念来实现这一点?

  • 问题内容: 如何简单地从本地Python(3.0)脚本SSH到远程服务器,提供登录名/密码,执行命令并将输出打印到Python控制台? 我宁愿不使用任何大型外部库或在远程服务器上安装任何东西。 问题答案: 我没有尝试过,但是这个pysftp模块可能会有所帮助,而后者又使用了paramiko。我相信一切都在客户端。 有趣的命令可能是在远程计算机上执行任意命令。(该模块的功能和方法也更多地暗示了它的F

  • 问题内容: 在哪里可以找到有关WDS的文档,特别是使用C#中的SQL查询来查询WDS?是否有任何资源列出可以从SystemIndex查询的列?另外,我希望查询返回“上下文”,即就像WDS客户端从找到搜索词的文档中的几行开始一样。尽管我相信3+的API是相同的,但我正在使用WDS 4.0。我检查了MSDN和其他站点,但是没有运气。 问题答案: 在MSDN论坛上发布并得到答案:可以搜索的列或属性:ht

  • 问题内容: 说我有一个类似的任务: 并行化每个compute()的最简单方法是什么(假设它们已经可以并行化了)? 我不需要严格匹配上面代码的答案,而只是一个常规答案。但是,如果您需要更多信息:我的任务是IO绑定的,这是针对Spring Web应用程序的,这些任务将在HTTP请求中执行。 问题答案: 我建议看一下ExecutorService。 特别是这样的事情: 请注意,如果列表很大,使用可能会很

  • 嗨,这让我感到困惑,我正在尝试搜索我的文件中的选定数字,然后用该行的其余部分显示该数字,问题是我的程序似乎在我搜索它时返回第一行内容,但当我键入搜索它们时不会返回第二行或第三行 我的 txt 文件内容 我正在寻找的输出 当我键入2或3以查找项目时,我的程序不显示任何问题,我该如何解决它?

  • 问题内容: 我正在尝试使用php创建一个脚本,该脚本将在文本文件中搜索并获取整行并回显它。 我有一个名为“ numorder.txt”的文本文件(.txt),并且在该文本文件中有几行数据,每5分钟会有新行出现(使用cron作业)。数据类似于: 我将如何创建一个php脚本来搜索数据“ aullah1”,然后抓起整行并回显它?(一旦回显,它应该显示“ 2 aullah1”(不带引号)。 如果我没有清楚

  • 我需要使用pymongo用python搜索一个ObjectId,但我总是得到这个错误。你知道怎么搜索吗?