Obviously one could loop through a file using fgetl or similar function and increment a counter, but is there a way to determine the number of lines in a file without doing such a loop?
相关问题
- Extract matrix elements using a vector of column i
- How do you get R's null and residual deviance
- How to display an image represented by three matri
- Lazily Reading a File in D
- OpenCV - Is there an implementation of marker base
相关文章
- How to replace file-access references for a module
- How do I append metadata to an image in Matlab?
- Why is file_get_contents faster than memcache_get?
- Transactionally writing files in Node.js
- How can I write-protect the Matlab language?
- `std::sin` is wrong in the last bit
- Escape sequence to display apostrophe in MATLAB
- Vertical line fit using polyfit
You can read the entire file at once, and then count how many lines you've read.
I like to use the following code for exactly this task
It is pretty fast if you have enough memory to read the whole file at once. It should work for both Windows- and Linux-style line endings.
Edit: I measured the performance of the answers provided so far. Here is the result for determining the number of lines of a text file containing 1 million double values (one value per line). Average of 10 tries.
So fastest are the approaches using Perl and reading all the file as binary data. I would not be surprised, if Perl internally also read large blocks of the file at once instead of looping through it line by line (just a guess, do not know anything about Perl).
Using a simple
fgetl()
-loop is by a factor of 25-75 slower than the other approaches.Edit 2: Included Edric's 2nd approach, which is much faster and on-par with the Perl solution, I'd say.
I found a nice trick here:
where
'countlines.pl'
is a perl script, containingI would recommend using an external tool for this. For example an app called
cloc
, which you can download here for free.On linux you then simply type
cloc <repository path>
and getThey also claim it should work on windows.
I think a loop is in fact the best - all other options so far suggested either rely on external programs (need to error-check; need str2num; harder to debug / run cross-platform etc.) or read the whole file in one go. Loops aren't so bad. Here's my variant
EDIT: Jonas rightly points out that the above loop is really slow. Here's a faster version.
It's still not as fast as
wc -l
, but it's not a disaster either.