Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get line count in a file without reading [duplicate]

Tags:

c#

Possible Duplicate:
How to count lines fast?

I have some files that contains data line by line.

I want to get the line count in a file to show progress state to user. (I process these files in background reading line by line)

I can do this by reading the file completely, but these files are so big that my application unnecessarily consumes RAM space.

So I want to get line count in a file without reading the whole file.

How can I do this?

like image 941
Uğur Aldanmaz Avatar asked Dec 02 '22 20:12

Uğur Aldanmaz


1 Answers

  1. Read the size (in bytes) of the file -- the o/s will tell you this.
  2. Read the first 1000 lines (and process them).
  3. Calculate the average line size.
  4. Divide this average size into the file size.
  5. Now you have an estimate of the number of lines in the file, accurate enough for a progress bar display sort of thing.
  6. If this is not accurate enough, recompute every now and then as you read the file.
like image 182
High Performance Mark Avatar answered Dec 04 '22 10:12

High Performance Mark