C++编程问题?

将所有代码写在主函数中:打开一个任意类型的文 件,统计每种字节(0 - 255)的出现次数,按字节值从小到 大的顺序,输出出现次数大于 0 的字节值及对应的出现次数。

#include<iostream>
#include<fstream>

using namespace std;

int main()
{
int i,arr[256]={0};
char c;
string path;
ifstream in;

cout<<"请输入要打开的文件的路径:";
cin>>path;
in.open(path.c_str(),ios_base::in|ios_base::binary);
while(!in.eof()&&in.get(c))
{
arr[(unsigned char)c]++;
}
for(i=0;i<256;i++)
{
if(arr[i])
cout<<i<<":"<<arr[i]<<endl;
}
in.close();
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-11-11
#include <stdio.h>
int main()
{
FILE* fp = fopen("name.txt", "rb");
rewind(fp);
int str[256] = {};
while (!feof(fp))
++str[(unsigned short)fgetc(fp)];
fclose(fp);
int i;
for (i = 0; i < 256; i++)
printf("%d:%d times\n", i, str[i]); //cout << i << ":" << str[i] << " times" << endl;
return 0;
}本回答被提问者采纳
相似回答