【问题标题】:Checking if one document has the contents of the other c++检查一个文档是否包含另一个 c++ 的内容
【发布时间】:2015-05-30 06:47:28
【问题描述】:

我正在编写一个代码来检查一个文档 (text1.txt) 中是否包含禁用词列表 (bannedwords.txt)。

例如,text1 文档包含一首歌的歌词,我想检查被禁止文档中的单词 pig 是否包含在其中。然后我希望输出类似于:

"pig" found 0 times
"ant" found 3 times

这是我到目前为止想出的,但似乎无法将禁用词数组放入搜索中。任何帮助都会很棒:D

谢谢菲茨

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

bool CheckWord(char* filename, char* search)
{
    int offset;
    string line;
    ifstream Myfile;
    Myfile.open(filename);

    if (Myfile.is_open())
    {
        while (!Myfile.eof())
        {
            getline(Myfile, line);
            if ((offset = line.find(search, 0)) != string::npos)
            {
                cout << "The Word  " << search<< " was found" << endl;
                return true;
            }
            else
            {
                cout << "Not found";
            }
        }
        Myfile.close();
    }
    else
        cout << "Unable to open this file." << endl;

    return false;
}

int main()
{
    ifstream file("banned.txt");
    if (file.is_open())//file is opened
    {
        string bannedWords[8];//array is created

        for (int i = 0; i < 8; ++i)
        {
            file >> bannedWords[i];
        }
    }
    else //file could not be opened
    {
        cout << "File could not be opened." << endl;
    }

    ifstream text1;//file is opened
    text1.open("text1.txt");

    if (!text1)//if file could not be opened
    {
        cout << "Unable to open file" << endl;
    }

    CheckWord("text1.txt", "cat");

    system("pause");
}

【问题讨论】:

  • 我们喜欢明确的问题。 “但似乎无法将禁用词数组放入搜索中”甚至是什么意思?请给出一些简短的输入文件和输出的清晰示例,有什么问题以及您不明白为什么会发生这种情况。
  • 提示:push_back()std::vector&lt;std::string&gt; bannedWords; 而不是使用固定大小的数组,并在 if/for 构造函数之外创建 bannedWords - 否则它将离开范围并被销毁在你想使用它之前。将其作为额外参数传递给CheckWord。在出现不可恢复的错误后,请致电 exit(EXIT_FAILURE); 而不是打印错误消息并尝试继续处理错误数据。使用while (getline(Myfile, line),不要测试while (...eof)
  • 您的问题是:“如何更改对 CheckWord 的调用以传递字符串数组?”。

标签: c++ arrays file search comparison


【解决方案1】:

您的main() 函数正在将banned.txt 的内容读入名为bannedWords 的8 个std::string 数组中。

数组bannedWords 此后不再使用。 C++ 不是靠魔法工作的,编译器也不是通灵的,所以无法读懂你的想法来理解你想让你的代码做什么。如果一个数组(或其元素)没有在任何地方被访问,它们将不会被用来做你想做的事情。

您需要将字符串从bannedWords 数组传递给CheckWord()。例如;

 CheckWord("text1.txt", bannedWords[0].c_str());

将尝试将bannedWords 中第一个字符串的内容传递给CheckWord()

但是,除非您将CheckWord()(名为search)的第二个参数设置为const,否则它也不会编译。

或者,更好的是,将第二个参数的类型更改为std::string 类型。如果这样做,则可以消除上面对c_str() 的使用。

我并没有声称这是您问题的完整解决方案 - 因为您的代码中有许多问题,有些与您所询问的内容有关,有些则不是。但是,我的建议可以帮助您入门。

【讨论】:

    【解决方案2】:

    你的问题真的很模糊;看来您需要花一些时间来确定您的程序结构,然后才能在这里寻求帮助。
    然而,由于我们都是新人,这里有一个合适的结构的建议: (我省略了文件处理位,因为它们与基本结构无关)

    //Populate your array of banned words
    std::string bannedWords[8];
    int i;
    for (int i = 0; i < 8; ++i)
    {
        file >> bannedWords[i];
    }
    
    //Load the entire file content into memory
    std::ifstream in("text1.txt");
    std::string fileContents((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
    

    所以现在整个文件内容都在字符串“fileContents”中,8个禁用词在“bannedWords”中。我建议采用这种方法,否则您将打开、阅读和关闭每个单词的文件。算不上好的设计。

    现在您必须根据文件内容检查每个单词。有一些更复杂的方法可以做到这一点,但最简单的选择是循环。

    //Loop through each banned word, and check if it's in the file
    for (int i = 0; i < 8; i++)
    {
        if (fileContents.find(bannedwords[i]) != std::string::npos)
        {
            //Do whatever
        }    
    }
    

    如果你想计算出现的次数,显然你需要做一些不同的查找,但这是另一个问题。

    【讨论】:

      猜你喜欢
      • 2021-08-31
      • 1970-01-01
      • 2017-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多