检索文件中的总行数 [英] Retrieving total line numbers in a file
本文介绍了检索文件中的总行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以告诉我如何使用编程语言 C 获取文本文件中的总行数吗?
can someone show me how to get the total number of lines in a text file in programming language C?
推荐答案
这是一种方法:
FILE* myfile = fopen("test.txt", "r");
int ch, number_of_lines = 0;
do
{
ch = fgetc(myfile);
if(ch == '
')
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 != '
' && number_of_lines != 0)
number_of_lines++;
fclose(myfile);
printf("number of lines in test.txt = %d", number_of_lines);
这篇关于检索文件中的总行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文