我如何找出文件中的行数 [英] How do i found out the number of lines in a file

查看:55
本文介绍了我如何找出文件中的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,当定义数组大小和此数组的初始化时,我定义文件中预期的行数。

。这个现在很好用,但是我相信将来这可能会改变。所以相比

比明确定义预期的数字,读取文件并以这种方式定义数字




所以我有一个我可以读入的csv文件,如何计算文件中

行的数量?有没有可以做到这一点的功能,或者我需要自己编码吗?

Vas。

Presently I define the number of lines to be expected in a file when
defining the array size and the initialisation of this array. This works
fine for now, but i''m sure that in the future this could change. So rather
than explicitly define the number expected, the file is read and the number
defined that way.

So I have a csv file that I can read in, how can I work out the number of
lines in the file? Is there a function that can do this, or do I need to
code this myself?

Vas.

推荐答案

" Vasilis Serghi" < VS ***** @ jaguar.com>写道:
"Vasilis Serghi" <vs*****@jaguar.com> writes:
所以我有一个可以读入的csv文件,如何计算出文件中的行数?是否有可以执行此操作的功能,
或者我是否需要自己编写代码?
So I have a csv file that I can read in, how can I work out the
number of lines in the file? Is there a function that can do this,
or do I need to code this myself?




只计算文件中\ n字符的数量(或者系统用于行尾的
字符组合)。


从你说的话看来你已经知道它的最大长度了

行。如果没有,你可以添加一个计数器来跟踪

最长线的长度,以确保分配足够的内存。


-

Stefano |博洛尼亚大学心理学系

Ghirlanda |斯德哥尔摩大学跨学科文化研究
http://www.intercult.su .se / ~stefano


2004年2月19日星期四10:24:55 -0000

" Vasilis Serghi" < VS ***** @ jaguar.com>写道:
On Thu, 19 Feb 2004 10:24:55 -0000
"Vasilis Serghi" <vs*****@jaguar.com> wrote:
现在我定义了在定义数组大小和初始化这个数组时文件中预期的行数。这个
目前工作正常,但我相信将来这可能会改变。


如果你确定它不会改变那么你可以肯定一些

abstrad会改变它:-)

因此,而不是明确定义预期的数量,读取文件并以这种方式定义
数字。

所以我有一个我可以读入的csv文件,怎么能我计算出文件中的行数?是否有可以执行此操作的功能,或者我是否需要自己编写代码?
Presently I define the number of lines to be expected in a file when
defining the array size and the initialisation of this array. This
works fine for now, but i''m sure that in the future this could change.
If you were sure that it would not change then you can be certain some
abstrad would change it :-)
So rather
than explicitly define the number expected, the file is read and the
number defined that way.

So I have a csv file that I can read in, how can I work out the number
of lines in the file? Is there a function that can do this, or do I
need to code this myself?




您必须通过计算行来自行完成当你读到它们时,如果你正在尝试将所有数据加载到内存中,那么你需要对空间进行malloc,然后根据需要重新分配。

-

Flash Gordon

支付成为一名极客&高级软件开发人员

虽然我的电子邮件地址是垃圾邮件,但它是真实的,我读了它。



You will have to work it out yourself by counting the lines as you read
them in. If you are trying to load all of the data in to memory then you
will need to malloc the space then realloc as necessary.
--
Flash Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spam, it is real and I read it.


Stefano Ghirlanda< st *** ************@unibo.it>这样说:
Stefano Ghirlanda <st***************@unibo.it> spoke thus:
从你说的话来看,你似乎已经知道了
线的最大长度。如果没有,你可以添加一个计数器来跟踪
最长线的长度,以确保分配足够的内存。
From what you say it seems you already know the maximum length of a
line. If not, you can add a counter to keep track of how long the
longest line is, to be sure to allocate enough memory.




谁需要分配内存? fgetc()可以很好地处理这项工作。


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。



Who needs to allocate memory? fgetc() can handle this job quite well.

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


这篇关于我如何找出文件中的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆