如何计算文本文件中特定单词的出现次数?
How do I count the number of occurrences of a specific word in a text file
我被我正在做的一项任务难住了,甚至不知道从哪里开始,我想我所拥有的可能完全没用。
我正试图读取一个包含8行文本的文本文件,每个文本中都有"行"这个词。我需要计算单词"line"在文件中出现的总次数。
到目前为止我写的代码
ifstream file("output.txt");
int wcount = 0;
string token;
string word(line);
while (file>>token)
if (word == token)
wcount++;
cout << wcount << endl;
我已经看了几个小时了,寻找了每一个可能的解决方案,但什么也没找到。请帮助。
改变这一行:
string word(line);
string word("line");
检查文件是否成功打开…
ifstream file("output.txt");
if ( !file )
{
// Deal with error.
}
// Read the contents of the file.
检查单词是否被正确读取…
while (file>>token)
{
std::cout << "Read the token: `" << token << "'" << std::endl;
if (word == token)
wcount++;
}
相关文章:
- 3-3. 编写一个程序来计算每个不同单词在其输入中出现的次数
- 计算单词中的音节数,但考虑到单词中彼此相邻的任何元音都算作一个音节
- 给定一个单词数组和一个字符串,如何计算给定字符串中的所有单词
- C++ 计算数组中的单词
- 计算一个单词在映射中出现的次数 - 使用 transform() - 调用不匹配
- 使用结构数组计算文本文件中单词的出现次数C++
- 创建程序以从给定的.txt文件中查找文本,替换并计算单词
- 计算单词中的字母
- 计算段落中出现次数最多的单词
- 我如何计算一个单词在数组中重复的次数
- 如何计算C++字符数组中的单词
- 如何使用二叉搜索计算搜索单词所需的时间
- 在.txt文件上计算重复单词的C 程序
- 计算本地文本文件中的平均单词长度
- 以 C++ 为单位计算字符串中的唯一单词
- 如何计算两个文件中字符单词和行的总数,并将它们相加以得出总数
- 在 C++11 中计算字母和单词的双字母组合的 std::线程向量的问题
- 使用C++计算字符串中字母数字单词数量的最有效方法
- 如何计算出现在字符串和输出行中的唯一单词?C
- 奇怪的添加 - 计算单词在文件中出现的次数