我正在尝试使用两个不同的文本文件制作程序。其中一个包含我想分析的实际文本,另一个包含单词列表。该程序应该检查文本中的单词何时出现在文本中并计算该单词。这里是(非工作)的代码,我到目前为止有:计算某些文字在C++文本文件中出现的次数
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
string word1;
string word2;
int listHits = 0;
ifstream data1 ("text.txt");
if (! data1) {
cout << "could not open file: " << "text.txt" << endl;
exit (EXIT_FAILURE);
}
ifstream data2 ("list.txt");
if (! data2) {
cout << "could not open file: " << "list.txt" << endl;
exit (EXIT_FAILURE);
}
while (data1 >> word1) {
while (data2 >> word2) {
if (word1 == word2) {
listHits++;
}
}
}
cout << "Your text had " << listHits << " words from the list " << endl;
system("pause");
return 0;
}
的text.txt如果包含
这里是一个文本。它将被加载到程序中。
和LIST.TXT包含
会
预期的结果是3。但是,不管是什么在文本文件中的程序总是给我的答案0。我已经检查过该程序实际上设法通过计算循环次数来读取文件,并且它可以工作。
在此先感谢
嗯,它并没有真正的工作,但我感谢帮助。现在我至少知道是什么导致了这个问题 – ThePancakerizer 2013-04-05 08:55:19