Friday, 22 January 2016

How to count number of lines in text file in C++ - cppexamples

Leave a Comment


Questions:

How to count number of lines in text file in C++

Explanation:

In this program you will learn how to count number of line in a text file.


Code:

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void main() {

       fstream file;//creating txt file
       file.open("Reading.txt",ios::in); //opening file in reading mod Reading.txt file is located in your project folder
       int temp2 = 0; //counter
       cout << "Data In Reading File" << endl;
       while (file) {
              string temp;
              getline(file, temp);
              cout << temp << endl;
              temp2++;
       }
       cout << "Number of Lines == " << temp2<<endl;

}


Output:

How to count number of lines in text file in C++
Reading.txt



How to count number of lines in text file in C++
Code Output


Related Articles:

C Plus Plus Program to Create a File & Store Information

C Plus Plus Program to Illustrate Reading of Data from a File


If You Enjoyed This, Take 5 Seconds To Share It