Đọc file ký tự bởi ký tự, đếm ký tự newline \n để lấy số dòng.
Ví dụ
File test.txt:
Hello friends, how are you?
This is a sample file.
Output: Total number of lines are: 2
Chương trình C ví dụ
#include <stdio.h>
#define FILENAME "test.txt"
int main() {
FILE *fp; char ch; int lines=0;
fp = fopen(FILENAME, "r");
if (!fp) { printf("File not found"); return -1; }
while ((ch=fgetc(fp))!=EOF) if (ch=='\n') lines++;
fclose(fp);
printf("Total lines: %d\n", lines);
return 0;
}