第43课第二题

来源:互联网 发布:网络监控系统 编辑:程序博客网 时间:2024/05/29 19:12
#include <stdio.h>#include <stdlib.h>//第二题,统计一个文本文件中数字、空格、字母出现的次数,以及文件的字节数,并将结果输出,文本文件名在程序中输入//输入文件名字符串部分可以使用第一题提供的语句。int main(){    FILE *fp ;    int count=0,count_num=0,count_eng=0,count_space=0;    char ch,fname[20];    printf("文件名:");    gets(fname);    if ((fp=fopen(fname,"r"))==NULL)    {        printf("connot open\n");        exit(0);    }    while (!feof(fp))    {        ch=fgetc(fp);        count++;        if(ch==' ')            count_space++;        else if(ch>='0'&&ch<='9')            count_num++;        else if((ch>='A'&&ch<='z')||(ch>='a'&&ch<='z'))            count_eng++;    }    printf("文件%s中包含%d个字母%d个数字%d个空格,共%d个字节",fname,count_eng,count_num,count_space,count);    fclose(fp);    return 0;}

0 0
原创粉丝点击