c语言中统计文件行数

 

1、

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

int main(void)
{
    int ch;
    FILE * fp;
    char filename[128];
    int lines;
    char tail;

    printf("please input the filename: ");
    scanf("%s", filename);
    
    fp = fopen(filename, "r");
    
    if(fp == NULL)
    {
        printf("file oprn failed.\n");
        exit(1);
    }
    
    while((ch = fgetc(fp)) != EOF)
    {
        putchar(ch);
        if(ch == '\n')
            lines++;
        tail = ch;
    }
    
    if(tail != '\n')
        lines++;
    printf("\n\nlines: %d.\n", lines);
    
    return 0;
}

c语言中统计文件行数

 

上一篇:谈谈弱网络环境下的视频传输


下一篇:C经典书籍笔记——c陷阱与缺陷②(语法陷阱之函数声明)