学c语言做练习之?统计文件中字符的个数
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
char ch;
FILE *fp;
long count=0;
if(argc !=2)
{
printf("文件名是:%s\n",argv[0]);
exit(EXIT_FAILURE);
}
if ((fp=fopen(argv[1],"r+"))==NULL)
{
fprintf(stderr,"不能打开文件\"%s\"\n",argv[1]);
exit(EXIT_FAILURE);
}
while((ch=getc(fp)) !=EOF)
{
putc(ch,stdout);
++count;
}
fclose(fp);
printf("File %s has %ld characters\n",argv[1],count);
return 0;
}
统计文件中字符个数(不采用命令行参数)
#include<stdio.h>
#include<stdlib.h>
#define MAX 80
int main(void)
{
FILE *fp;
char ch;
char name[MAX];
long count=0;
printf("请输入文件名:");
gets(name);
if ((fp=fopen(name,"r+"))==NULL)
{
fprintf(stderr,"不能打开文件%s\n",name);
exit(EXIT_FAILURE);
}
while((ch=getc(fp))!=EOF)
{
putc(ch,stdout);
count++;
}
fclose(fp);
printf("File %s has %ld characters\n",name,count);
return 0;
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。