C fgetc与fgets

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void myCount(char *filename);
void maxLine(char *filename);
int main(){

    char *filename="D:/23.txt";
    myCount(filename);
    maxLine(filename);
    return 1;
}

void myCount(char * filename){
    FILE * fp;
    fp=fopen(filename,"r");

    char ch;
    int countLine=1;
    int Uletters=0;
    int Sletters=0;
    int others=0;
    while( (ch=fgetc(fp))&&ch!=EOF  ){//EOF是一个char常量  表示读到了文件末位
        //printf("%c,",ch);//ok
        if(ch=='\n'){
            countLine++;
        }else if(ch==' '){

        }else if( ch>='a'&&ch<='z'  ){
            Uletters++;
        }else if( ch>='A'&&ch<='Z' ){
            Sletters++;
        }else {
            others++;
        }
    }
}

void maxLine(char * filename){
    FILE * fp;
    fp=fopen(filename,"r");

    char temp[100];
    char maxLine[100];
    int max=0;
    while(  !feof(fp)  ){//用foef(fp)函数来判断是否是文件末尾
        fgets(temp,100,fp);//实际上最多读入99个字符 因为读入的一行的末位要存放'\0'
        puts(temp);
        if(strlen(temp)>max){
            max=strlen(temp);
            strcpy(maxLine,temp);
        }
    }
    printf("maxLine is %s",maxLine);
}

转载于:https://www.cnblogs.com/cart55free99/archive/2013/03/25/2981043.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值