小编cos*_*mic的帖子

使用C++计算文本文件中的字母数字字符

我写了一个程序来计算文本文件中的字母数字字符数.但是,它返回的数字总是大于在线字符计数器返回的数字.

例如,程序将计算此文本中的字母数字字符数:

如果这些人有奇怪的时尚和对最特别的事情的期望服从,他们至少准备为他们的怪癖付出代价

再次运行程序,它会说文本中有164个字符.再次运行,它会说有156个字符.使用这个在线字符计数器,似乎字符数应该低于144(在线字符计数器也包括空格).

这是代码:

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

int main() {
    char line[100];
    int charcount = 0;
    ifstream file("pg1661sample.txt");
    while (!file.eof()) {
        file.getline(line, 99);
        for (int i = 0; i < 100; i++) {
            if (isalnum(line[i])) {
                charcount++;
            }
        }
    }

    cout << endl << "Alphanumeric character count: " << charcount;
    cin.get();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

c++ alphanumeric text-files

1
推荐指数
1
解决办法
3808
查看次数

标签 统计

alphanumeric ×1

c++ ×1

text-files ×1