Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to find the number of lines in a text (C++)

Tags:

People also ask

How do I count the number of lines in a text file C#?

The simplest way to get the number of lines in a text file is to combine the File. ReadLines method with System. Linq. Enumerable.

How do you count lines in a text?

The most easiest way to count the number of lines, words, and characters in text file is to use the Linux command “wc” in terminal. The command “wc” basically means “word count” and with different optional parameters one can use it to count the number of lines, words, and characters in a text file.

How do you find the number of lines?

The wc command is used to find the number of lines, characters, words, and bytes of a file. To find the number of lines using wc, we add the -l option. This will give us the total number of lines and the name of the file.


I need to read the number of lines in a file before doing some operations on that file. When I try to read the file and increment the line_count variable at each iteration until i reach eof. It was not that fast in my case. I used both ifstream and fgets . They were both slow . Is there a hacky way to do this, which is also used by, for instance BSD, Linux kernel or berkeley db.(may be by using bitwise operations).

As I told before there are millions of lines in that file and it keeps get larger, each line has about 40 or 50 characters. I'm using Linux.

Note: I'm sure there will be people who might say use a DB idiot. But briefly in my case i can't use a db.