c语言 13-6 利用fgetc函数输出文件的行数

1、

#include <stdio.h>

int main(void)
{
    FILE *fp;
    int ch;
    char filename[FILENAME_MAX];
    printf("Please input the filename: "); scanf("%s", filename);
    
    if((fp = fopen(filename, "r")) == NULL)
        printf("\aFile open failed.\n");
    else
    {
        int i = 0;
        while((ch = fgetc(fp)) != EOF)
        {
            if(ch == '\n')
                i++;
        }
        fclose(fp);
        printf("This file has %d line(s).\n", i);
    }
    return 0;
}

 

posted @ 2021-06-11 09:13  小鲨鱼2018  阅读(101)  评论(0编辑  收藏  举报