#include<stdio.h>
#include<stdlib.h>
void input(char b[]);
void count(FILE *fp);
void main()
{
int x;
char ch,st,a[50];
FILE *FP;
do{
printf("plese select function:input:(1),statistics:(2)");
scanf("%d",&x);
if(x==1)
{ printf("please input some charaters:\n");
scanf("%s",a);
input(a);}
if(x==2)
{ if((FP=fopen("statistics.txt","r"))==NULL)
printf("file can not open!\n");
count(FP);}
printf("continue?(Yes or No)");
scanf("%c%c",&st,&ch);
}
while(ch=='y');
}
void input(char b[])
{
FILE *fp;
if((fp=fopen("statistics.txt","a"))==NULL)
{ printf("file can not open!\n");
exit(1);
}
fputs(b,fp);
fclose(fp);
return;
}
void count(FILE *fp)
{
int n=0,m=0,i=0,j=0;
char CH;
if((fp=fopen("statistics.txt","r"))==NULL)
{ printf("file can not open!\n");
exit(1);
}
while(!feof(fp))
{ CH=fgetc(fp);
if((CH>='A'&&CH<='Z')||(CH>='a'&&CH<='z'))
n++;
else if(CH==' ')
m++;
else if(CH>='0'&&CH<='9')
i++;
else j++;
}
printf("char num:%d\n",n);
printf("space num:%d\n",m);
printf("digit num:%d\n",i);
printf("other num:%d\n",j);
fclose(fp);
return;
}
功能:如果选择‘1’能将用户输入的数据以追加的方式存入statistics.txt文件,选择‘2’统计文件中数字,空格,字母,其他字符的个数;然后由用户选择继续或结束;
请高手帮忙指点指点,滴水之恩,涌泉相报
[求助]数据的录入和统计