检查一个文档是否包含另一个c++的内容
Checking if one document has the contents of the other c++
我正在写一个代码来检查一个文档(text1.txt)中是否包含禁止单词列表(bannedwords.txt)。
例如,text1文档包含一首歌的歌词,我想检查被禁止文档中的单词pig是否包含在其中。然后我希望输出类似于:
"pig" found 0 times
"ant" found 3 times
这就是我到目前为止所想到的,但似乎无法将一系列被禁止的单词放入搜索中。任何帮助都将是惊人的:D
感谢Fitz
#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");
}
您的main()
函数正在将banned.txt
的内容读取到名为bannedWords
的8个std::string
的数组中。
在那之后的任何地方都不会使用数组bannedWords
。C++不能神奇地工作,编译器也不是通灵的,所以不能读懂你的想法来理解你想让代码做什么。如果一个数组(或其元素)在任何地方都不能访问,它们就不会被用来做你想用它们做的事。
您需要将字符串从bannedWords
数组传递到CheckWord()
。例如
CheckWord("text1.txt", bannedWords[0].c_str());
将尝试将CCD_ 8中的第一个字符串的内容传递给CCD_。
但是,除非使CheckWord()
的第二个参数(名为search
)为const
限定参数,否则这两个参数都不会编译。
或者,更好的是,将第二个参数的类型更改为std::string
类型。如果你这样做,你就可以在上面消除c_str()
的使用。
我并不认为这是你问题的完整解决方案,因为你的代码中有很多问题,有些与你所询问的内容有关,有些则不然。然而,我的建议会让你开始。
你的问题真的很模糊;看起来你需要花一些时间来确定你的程序结构,然后才能在这里寻求帮助
然而,由于我们都是新手,这里有一个合适的结构建议:(我省略了文件处理位,因为它们与基本结构无关)
//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
}
}
显然,如果你想统计出现的次数,你需要用不同的方法来寻找,但这是另一个问题。
- 在作为静态成员包含在另一个类中的类的构造函数中使用 cout
- 检查路径是否包含C++中的另一个路径
- C++ .h 包含从一个类传递到另一个类的类和.cpp
- CMake 将库包含在另一个库中
- 如何将包含另一个对象向量的对象保存到文件中,并使用C++中的二进制文件从文件中读回?
- 创建一个 const std::vector ,其中包含另一个 const std::vector 和其他值
- 包含来自另一个文件的函数会导致范围错误(openFoam)
- 如何为包含另一个类实例的数组制作常量 getter?
- 使用按位运算确定值是否包含另一个值
- C++将包含的库 (ref DLL) 从根目录更改为另一个位置
- 如何在我的 cmake 项目中包含另一个 cmake 项目的头文件?
- 继承一个类,该类继承另一个已包含的类
- 如何查找哪个标头在包含树中包含另一个标头
- 将 2 个链表中包含的相同值写入另一个链表
- 如何从另一个包含目录中的标头访问包含目录中的标头
- 如何将另一个项目中用 C 编写的源代码包含在我自己的项目中,C++在 Visual Studio 中
- 包含另一个实例的特征实例,该实例持有固定大小的特征对象
- 如何隐藏另一个包含的静态库的API
- 如何从文件中的类调用,以使用着色器显示在另一个包含gl代码的文件中(其中两个文件都在jni文件夹中)
- 如何在另一个包含对象的文件中创建一个函数并从 main 调用它