首页 > 其他 > 详细

第四次作业

时间:2018-05-04 22:52:55      阅读:213      评论:0      收藏:0      [点我收藏+]
#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);

第四次作业

原文:https://www.cnblogs.com/chenxidream/p/8992880.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!