Most Compact Way to Count Number of Lines in a File in C++

10,628

Solution 1

FILE *f=fopen(filename,"rb");

int c=0,b;while ((b=fgetc(f))!=EOF) c+=(b==10)?1:0;fseek(f,0,SEEK_SET);

Answer in c. That kind of compact?

Solution 2

If the reason you need to "go back again" is because you cannot continue without the size, try re-ordering your setup.

That is, read through the file, storing each line in a std::vector<string> or something. Then you have the size, along with the lines in the file:

#include <fstream>
#include <iostream>
#include <string>
#include <vector>

int main(void)
{
    std::fstream file("main.cpp");
    std::vector<std::string> fileData;

    // read in each line
    std::string dummy;
    while (getline(file, dummy))
    {
        fileData.push_back(dummy);
    }

    // and size is available, along with the file
    // being in memory (faster than hard drive)
    size_t fileLines = fileData.size();

    std::cout << "Number of lines: " << fileLines << std::endl;
}

Here is a solution without the container:

#include <fstream>
#include <iostream>
#include <string>
#include <vector>

int main(void)
{
    std::fstream file("main.cpp");
    size_t fileLines = 0;    

    // read in each line
    std::string dummy;
    while (getline(file, dummy))
    {
        ++fileLines;
    }

    std::cout << "Number of lines: " << fileLines << std::endl;
}

Though I doubt that's the most efficient way. The benefit of this method was the ability to store the lines in memory as you went.

Solution 3

I think this might do it...

std::ifstream file(f);
int n = std::count(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>(), '\n') + 1;

Solution 4

#include <stdlib.h>
int main(void) { system("wc -l plainfile.txt"); }

Solution 5

Count the number of instances of '\n'. This works for *nix (\n) and DOS/Windows (\r\n) line endings, but not for old-skool Mac (System 9 or maybe before that), which used just \r. I've never seen a case come up with just \r as line endings, so I wouldn't worry about it unless you know it's going to be an issue.

Edit: If your input is not ASCII, then you could run into encoding problems as well. What's your input look like?

Share:
10,628
Danf
Author by

Danf

Updated on June 27, 2022

Comments

  • Danf
    Danf almost 2 years

    What's the most compact way to compute the number of lines of a file? I need this information to create/initialize a matrix data structure.

    Later I have to go through the file again and store the information inside a matrix.

    Update: Based on Dave Gamble's. But why this doesn't compile? Note that the file could be very large. So I try to avoid using container to save memory.

    #include <iostream>      
    #include <vector>        
    #include <fstream>       
    #include <sstream>       
    using namespace std;     
    
    
    int main  ( int arg_count, char *arg_vec[] ) {
        if (arg_count !=2 ) {
            cerr << "expected one argument" << endl;
            return EXIT_FAILURE;      
        }
    
        string line;
        ifstream myfile (arg_vec[1]);
    
        FILE *f=fopen(myfile,"rb");
        int c=0,b;
        while ((b=fgetc(f))!=EOF) c+=(b==10)?1:0;
        fseek(f,0,SEEK_SET);
    
    
        return 0;
    }