0001 unsigned int * stat( char* sample_text_file ) { //统计字符出现频率 0002 unsigned int * freq = new unsigned int[N_CHAR]; //以下统计需随机访问,故以数组记录各字符出现次数 0003 memset ( freq, 0, sizeof ( int ) * N_CHAR ); //清零 0004 FILE* fp = fopen ( sample_text_file, "r" ); //assert: 文件存在且可正确打开 0005 for ( char ch; 0 < fscanf ( fp, "%c", &ch ); ) //逐个扫描样本文件中的每个字符 0006 if ( (0x20 <= ch) && (ch < 0x80) ) freq[ch - 0x20]++; //累计对应的出现次数 0007 fclose ( fp ); return freq; 0008 }