想问一下大佬们c语言统计文件里面不同类型的字符个数

没办法把文件里面的数字字母和其他字符分开提取,有大佬帮一下忙吗

 

供参考:

#include <stdio.h>

int main()
{
 FILE *fp;
 int  num=0,others=0,chars=0;
 char ch;
 fp=fopen("bookdata.txt","r");
 if(fp==NULL)
 {
    printf("Open file error!\n");
    exit (0);
 }
 while(1)
 {
    if(fscanf(fp,"%c",&ch) != 1) break;
    if(ch >= '0' && ch <= '9')
         num++;
    else if((ch >= 'A'&& ch <= 'Z')||(ch >= 'a'&& ch <= 'z'))
         chars++;
    else others++;
 }
 fclose(fp);
 printf("num:%d chars:%d others:%d\n",num,chars,others);
 
 return 0;
}

 

if(a>=0&&a<=9)这里,应该是if(a>='0'&&a<='9'),这样才是数字字符

这个是#include <stdio.h>

int main(void)

{

 FILE *p;

 int num;

 char chars,a;

 int others;

 num=0;

 chars=0;

 others=0;

 p=fopen("d:/Êý×Ö×Öĸ×Ö·û.txt","r");

 if(p==NULL)

 {

  printf("Open error\n");

  exit (0);

 }

    while(1)

 {

  fscanf(p,"%c",&a);

  if(a>=0&&a<=9)

   num++;

  else if((a>=65&&a<=90)||(a>=97&&a<=122))

   chars++;

  else others++; 

  if(feof(p))

   break;

 }

 fclose(p);

 printf("num:%d chars:%d others:%d\n",num,chars,others);

 return 0;

}