读取一个文件中的字符,统计每个字符出现的次数

 1 //统计每个字符出现的次数
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 #include<string.h>
 5 
 6 int main()
 7 {
 8     FILE *fp_read;
 9     char ch;
10     int count[26];
11     int index;
12     fopen_s(&fp_read,"E:\first.txt","r");
13     memset(count,0,sizeof(count));
14     if(NULL == fp_read)
15     {
16         printf("Can not open the file!
");
17         system("pause");
18         return 0;
19     }
20 
21     while(!feof(fp_read))
22     {
23         ch = fgetc(fp_read);
24         if(ch >= 'a' && ch <= 'z')
25             count[ch - 'a']++;
26     }
27     for(index = 0; index < 26; index++)
28         if(count[index] != 0)
29             printf("%c:	%d
",index+97, count[index]);
30 
31     fclose(fp_read);
32 
33     system("pause");
34     return 0;
35 }
原文地址:https://www.cnblogs.com/cpsmile/p/4776870.html