How to count lines of a file in C++?

malhobayyeb picture malhobayyeb · Jun 18, 2010 · Viewed 91.7k times · Source

How can I count lines using the standard classes, fstream and ifstream?

Answer

Abhay picture Abhay · Jun 18, 2010

How about this :-

  std::ifstream inFile("file"); 
  std::count(std::istreambuf_iterator<char>(inFile), 
             std::istreambuf_iterator<char>(), '\n');