C++字典 与多个字符串和文件进行比较
C++ Dictionary Compare with multiple strings and files
我正在尝试编写一个程序,将输入文件与充满大量单词的字典文件进行比较。比较单词后,我想输出拼写错误的单词。这是我的代码:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
using namespace std;
void trim(string s)
{
size_t p = s.find_first_not_of(" t");
s.erase(0, p);
p = s.find_last_not_of(" t");
if (string::npos != p)
s.erase(p+1);
}
int main()
{
ifstream input;
ifstream words;
input.open("/Users/jordan/Desktop/CS60/Word_dictionary_check/input.txt");
if(input.fail())
{
cout<<"Input file opening failed";
exit(1);
}
words.open("/Users/jordan/Desktop/CS60/Word_dictionary_check/words.txt");
if(words.fail())
{
cout<<"Words file opening failed";
}
vector <string> wordCheck;
vector <string> misspelledWord;
string temp = "";
while(!input.eof())
{
input>>temp;
wordCheck.push_back(temp);
}
ofstream output;
output.open("/Users/jordan/Desktop/CS60/Word_dictionary_check/output.txt");
if(output.fail())
{
cout<<"Output file opening failed";
exit(1);
}
for(int j = 0; j < wordCheck.size(); j++)
{
bool dontprint = false;
while(!words.eof())
{
words>>temp;
if(temp == wordCheck[j])
{
dontprint = true;
}
}
if(dontprint == false)
{
misspelledWord.push_back(wordCheck[j]);
}
}
for(int i = 0; i < misspelledWord.size() ; i++)
{
output<<misspelledWord[i]<<endl;
}
return 0;
}
我相信带有空格或字符串比较的东西是一个问题。 谢谢你帮助我!
我可以看到几个明显的问题。我添加了评论。这应该可以解决您的问题,但我不会为您编写代码。
for(int j = 0; j < wordCheck.size(); j++)
{
bool dontprint = false;
//Make your words file pointer to point to start of file. USe seek function
while(!words.eof())
{
words>>temp;
if(temp == wordCheck[j])
{
dontprint = true;
//You can break here. As once word is found, you don't need to check the word file further
}
}
if(dontprint == false)
{
misspelledWord.push_back(wordCheck[j]);
}
}
相关文章:
- C++将目录中的所有文件与::filesystem进行比较
- catch框架有没有办法比较流或文件
- 从文件中读取多个字节,并将它们存储在C++中进行比较
- 将字符串与文件上的数据进行比较
- 通过比较C++中的行在 txt 文件中搜索的最简单方法是什么?
- 比较文本文件的元素
- 比较文本文件的元素
- 如何从目录迭代器中选择特定文件并进行比较
- 比较两个没有一行共同点的大文件
- 我正在尝试从输入文件中读取,然后使用它们的子字符串比较字符串的特定部分
- 比较文件中的最佳平均成绩并打印学生的姓名
- 如何在 c++ 中比较两个文本文件
- 如何在 c++ 中比较多个.txt文件?
- 使用-L和-L标志与将库文件作为输入进行比较
- 如何将新的字符串名称与现有的txt文件进行比较
- 如何比较文件中包含的下一个字符
- 如何比较time_t和标准::文件系统::file_time_type
- 比较两个文件夹的关系
- 比较 JSON::Value 变量中的数据,然后更新到文件
- 有没有一种更简单的方法可以像在java中使用包一样在C++中重用代码?制作头文件比较复杂