#include<iostream> #include <algorithm> //For std::count #include <iterator> //For istream_iterator #include <fstream> //For file operation /* Desciption: For counting number of line in a file we have used std::count funtion which is defined in <alogrithm> It take starting and ending iterator of the file and a value which is to be matched. In our case that value is new line ('\n') */ int main() { std::ifstream fp("sample.txt"); //Stopping new line for skipping fp.unsetf(std::ios_base::skipws); unsigned int count = std::count(std::istream_iterator<char>(fp),std::istream_iterator<char>(),'\n'); fprintf(stdout,"Total number of line in file are: %d \n",count);
fp.close();
} /* Output: Total number of line in file are: 19 */
Comments