如何快速确定文本文件中的行数?
我是使用 C 编程的新手,我想知道是否有一个函数调用可用于快速确定文本文件中的行数。
I'm new to using C programming I was wondering if there is a function call that can be used to quickly determine the amount of rows in a text file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不。不过有一个标准的 Unix 实用程序可以执行此操作,即
wc
。您可以查找wc
的源代码来获取一些指针,但它会归结为简单地从头到尾读取文件并计算行数/作品数/其他内容。No. There is a standard Unix utility that does this though,
wc
. You can look up the source code forwc
to get some pointers, but it'll boil down to simply reading the file from start to end and counting the number of lines/works/whatever.您必须编写自己的文件,并且您必须注意文件的格式...行是否以
\n
结尾?或\r\n
?如果最后一行不以换行符结尾(所有文件都应如此)怎么办?您可能会检查这些内容,然后计算文件中的换行符。You have to write your own, and you have to be conscious of the formatting of the file... Do lines end with
\n
? or\r\n
? And what if the last line doesn't end with a newline (as all files should)? You would probably check for these and then count the newlines in the file.不,没有。你必须自己写。
如果行大小固定,那么您可以使用 fseek 和 ftell 移动到文件末尾
然后计算它。
如果没有,您必须遍历文件计数行。
您是否想创建一个行数组?
像什么
?
No, theres not. You have to write your own.
If the line-size if fixed, then you could use fseek and ftell to move to the end of the file
and then calculate it.
If not, you have to go through the file counting lines.
Are you trying to create an array of lines?
Something like
?