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; }