码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-09-01 21:15:44      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:

 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!\n");
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:\t%d\n",index+97, count[index]);
30 
31     fclose(fp_read);
32 
33     system("pause");
34     return 0;
35 }

 

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

标签:

原文地址:http://www.cnblogs.com/cpsmile/p/4776870.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!