views:

276

answers:

5

Hi,

In C++, how can I count lines using the standard classes fstream, ifstream?

Thank you, Mohammad

+10  A: 

You read the file line by line. Count the number of lines you read.

Martin York
That sounds like a _huge_ overcomplication! ;-)
James McNellis
+13  A: 

How about this :-

  std::ifstream inFile("file"); 
  std::count(std::istreambuf_iterator<char>(inFile), 
             std::istreambuf_iterator<char>(), '\n');
Abhay
That's what I'd say, if you didn't say it before me. Still, your way is slightly better... +1
fingerprint211b
Just remembered something, What if the last line doesn't have a \n?
fingerprint211b
@fingerprint211b: Add one to the result :-) There is always this tradeoff whether u have a newline at the end of file.
Abhay
@fingerprint211b: Then there is one less line. Just because the last line is blank does not mean it's any different than any other line.
Billy ONeal
Beautiful and elegant. Love that STL.
wheaties
@wheaties: I also think think this is a faster way of counting lines in file if not the fastest. If you are reading lines, the system library (be it fgets or std::readline) will copy the data to *your* buffer; but an optimal solution will access the data in the buffer where it is read from the system, like memory mapping. So the truly optimal solution by the library could be to create a mmap_streambuf to mmap the file. A count_lines method would then just iterate over it there would never be a need to copy the data at all.
Abhay
I cannot find "std:count" I just included <iostream>, <string> and <fstream>.
MIH1406
@MIH: A quick Google search would have told you that `std::count()` is in `<algorithm>`.
James McNellis
@MIH1406: `#include <algorithm>`
Abhay
+1 for `istream` **buf** `_iterator`, as made famous by Item 29 of Effective STL!
Cubbi
A: 

int numLines = 0;
ifstream in("file.txt");
//while ( ! in.eof() )
while ( in.good() )
{
   std::string line;
   std::getline(in, line);
   ++numLines;
}

There is a question of how you treat the very last line of the file if it does not end with a newline. Depending upon what you're doing you might want to count it and you might not. This code counts it.

See: http://www.cplusplus.com/reference/string/getline/

Craig W. Wright
`while ( ! in.eof() )` <--- NOOOOO! This will give garbage if there's any type of failure reading the file. Put `std::getline` as the condition of the while.
Billy ONeal
It's still invalid after your edit because if `std::getline` fails, you increment the line count, and don't check if it succeeded until after. Move `std::getline` into the condition of the while.
Billy ONeal
A: 

This is the correct version of Craig W. Wright's answer:

int numLines = 0;
ifstream in("file.txt");
while ( std::getline(in, std::string()) )
   ++numLines;
Billy ONeal
A: 

Divide the file size by the average number of characters per line!

John
How does one determine the average number of characters per line without reading the file? If your counting characters, you might as well just count the newlines instead; which kind of invalidates your answer.
Thomas Matthews