我正在尝试从命令行读取文件,将字符读入数组,计算字符个性并打印结果。代码编译时没有任何错误,但单个字母计数远高于大文件应有的数量,有时对于小文件根本不计算在内。
#include <iostream>
#include <fstream>
#include <string.h>
#include <cctype>
#include <algorithm>
using namespace std;
int main(int argc, char **argv){
if(argc !=2){
cout << "usage: " << argv[0] << " <filename>\n";
}
else{
ifstream myfile (argv[1]);
if(!myfile.is_open()) //check to see if file is opened.
cout << "Can not open your file\n";
else{
static int array[26]; //stores the frequency of letters.
char t;
while(!myfile.eof()){
while(myfile.get(t)){
if(isupper(t)){
int i = 0;
do{
array[tolower(t)-'a']++;
i++;
}
while(i < 26);
}
}
int x = 0;
while(x < 26){
cout << 'a' + x << ":" << array[x] << endl;
x++;
}
}
}
}
return 0;
}