How to get line count in a file without reading

c#

Solution

- Read the size (in bytes) of the file -- the o/s will tell you this.

- Read the first 1000 lines (and process them).

- Calculate the average line size.

- Divide this average size into the file size.

- Now you have an estimate of the number of lines in the file, accurate enough for a progress bar display sort of thing.

- If this is not accurate enough, recompute every now and then as you read the file.

Problem

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?

Original source

Related problems