检索文件中的总行数

谁能告诉我如何用编程语言C获取文本文件中的总行数?

这是一种方法:

FILE* myfile = fopen("test.txt", "r"); int ch, number_of_lines = 0; do { ch = fgetc(myfile); if(ch == '\n') number_of_lines++; } while (ch != EOF); // last line doesn't end with a new line! // but there has to be a line at least before the last line if(ch != '\n' && number_of_lines != 0) number_of_lines++; fclose(myfile); printf("number of lines in test.txt = %d", number_of_lines); 

“不是由项目经理”解决方案

 system("wc profile.dat > no.lines"); FILE *pfile = fopen("no.lines", "r"); int lines; fscanf(pfile, "%d", &lines); system("rm no.lines");