标签:scanf fopen turn [] scan 文件的 file fgetc ret
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int zifushu(FILE *fp);
int cishu(FILE *fp);
int line(FILE *fp);
int main(int argc,char *argv[])
{
FILE *fp;
if((fp=fopen("file.c","r"))==NULL){
printf("cannot open file\n");
exit(0);
}
char a;
int count;
scanf("%c",&a);
if(a==‘c‘){
count=zifushu(fp);
}else if(a==‘w‘){
count=cishu(fp);
}else if(a==‘l‘){
count=line(fp);
}
printf("%d",count);
fclose(fp);
return 0;
}
int zifushu(FILE *fp){
/*FILE *fp;
if(fp=fopen(file.c,"r")==NULL){
printf("cannot open file\n");
exit(0);
}*/
char c;
int count=0;
while(!feof(fp)){
c=fgetc(fp);
if(c>=0&&c<=127){
count++;
}
}
//fclose(fp);
return count;
// printf("%d",count);
}
int cishu(FILE *fp){
/*FILE *fp;
if(fp=fopen(file.c,"r")==NULL){
printf("cannot open file\n");
exit(0);
}*/
char c;
int count=0;
while(!feof(fp)){
c=fgetc(fp);
if((c>=‘A‘&&c<=‘Z‘)||(c>=‘a‘&&c<=‘z‘)) count++;
}
//fclose(fp);
return count;
// printf("%d",count);
}
int line(FILE *fp){
/*FILE *fp;
if(fp=fopen(file.c,"r")==NULL){
printf("cannot open file\n");
exit(0);
}*/
char c;
int count=0;
while(!feof(fp)){
if(c==‘\n‘) count++;
}
//fclose(fp);
return count;
// printf("%d",count);
}
1.定义文件型指针变量
FILE *fp;
2.打开文件的方式
if((fp=fopen("file1",‘‘r")==NULL){
printf("cannot open file\n");
exit(0);
}
关闭文件:
fclose(fp);
标签:scanf fopen turn [] scan 文件的 file fgetc ret
原文地址:https://www.cnblogs.com/chenxidream/p/8992880.html