Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 917
malhobayyeb Avatar asked Jun 18 '10 20:06

malhobayyeb


People also ask

How do I count lines in a file?

Using “wc -l” There are several ways to count lines in a file. But one of the easiest and widely used way is to use “wc -l”. The wc utility displays the number of lines, words, and bytes contained in each input file, or standard input (if no file is specified) to the standard output. 1.

How do I count the number of lines in a shell file?

Use wc –lines command to count the number of lines. Use wc –word command to count the number of words. Print the both number of lines and the number of words using the echo command.

Is there a count function in C?

Program to Count the Number of Digitswhile loop is iterated until the test expression n! = 0 is evaluated to 0 (false). After the first iteration, the value of n will be 345 and the count is incremented to 1. After the second iteration, the value of n will be 34 and the count is incremented to 2.


1 Answers

How about this :-

  std::ifstream inFile("file");    std::count(std::istreambuf_iterator<char>(inFile),               std::istreambuf_iterator<char>(), '\n'); 
like image 185
Abhay Avatar answered Sep 21 '22 04:09

Abhay