有人可以告诉我如何用编程语言C获取文本文件中的总行数吗?
答案 0 :(得分:21)
这是一种方法:
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);
答案 1 :(得分:-5)
A"不是由项目经理"溶液
system("wc profile.dat > no.lines");
FILE *pfile = fopen("no.lines", "r");
int lines;
fscanf(pfile, "%d", &lines);
system("rm no.lines");