#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;
}