统计时出的问题
做哈夫曼编码的时候测试一下统计的代码,如下
代码:
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
int main(int argc, char **argv)
{
if(argc < 2)return -1;
ifstream ifile(argv[1]);
if(!ifile) return -1;
unsigned int w[256];
memset(w, 0, 256*sizeof(int));
w[255] = 0;
while(!ifile.eof()){
unsigned char in = ifile.get();
w[in]++;
}
for(int i=0;i<256;i++) cout << i << " with counts:" << w[i] << endl;
return 0
}
结果在文本文件的统计时总是多了一个255的值,也就是最后一行至少是1(可执行文件的时候有可能大于1),这个255是这么来的