Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 196545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:46:36+00:00 2026-05-11T16:46:36+00:00

I need to read the number of lines in a file before doing some

  • 0

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).

The number of lines is in the millions in that file and it keeps getting larger, each line is 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:46:36+00:00Added an answer on May 11, 2026 at 4:46 pm

    The only way to find the line count is to read the whole file and count the number of line-end characters. The fastest way to do this is probably to read the whole file into a large buffer with one read operation and then go through the buffer counting the ‘\n’ characters.

    As your current file size appears to be about 60Mb, this is not an attractive option. You can get some of the speed by not reading the whole file, but reading it in chunks, say of size 1Mb. You also say that a database is out of the question, but it really does look to be the best long-term solution.

    Edit: I just ran a small benchmark on this and using the buffered approach (buffer size 1024K) seems to be a bit more than twice as fast as reading a line at a time with getline(). Here’s the code – my tests were done with g++ using -O2 optimisation level:

    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <ctime>
    using namespace std;
    
    unsigned int FileRead( istream & is, vector <char> & buff ) {
        is.read( &buff[0], buff.size() );
        return is.gcount();
    }
    
    unsigned int CountLines( const vector <char> & buff, int sz ) {
        int newlines = 0;
        const char * p = &buff[0];
        for ( int i = 0; i < sz; i++ ) {
            if ( p[i] == '\n' ) {
                newlines++;
            }
        }
        return newlines;
    }
    
    int main( int argc, char * argv[] ) {
        time_t now = time(0);
        if ( argc == 1  ) {
            cout << "lines\n";
            ifstream ifs( "lines.dat" );
            int n = 0;
            string s;
            while( getline( ifs, s ) ) {
                n++;
            }
            cout << n << endl;
        }
        else {
            cout << "buffer\n";
            const int SZ = 1024 * 1024;
            std::vector <char> buff( SZ );
            ifstream ifs( "lines.dat" );
            int n = 0;
            while( int cc = FileRead( ifs, buff ) ) {
                n += CountLines( buff, cc );
            }
            cout << n << endl;
        }
        cout << time(0) - now << endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to read a file line by line, and change a variable accordingly.
I need to read and write some data on .mdb Access file and over
I need to parse some information from lines of texts that follow a certain
I'm having issues when I try to read a float number from a file.
I've the need of read a txt file that is structured in this way
I need to read in a file, each line of the file has one
I need to do processes on a file ,first count the number of lines
I need to search for certain lines in a file that contain numbers, then
I need to read the first n lines of a text file as lines
I need to read a large space-seperated text file and count the number of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.