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:
Reading.txt |
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
0 Questions:
Post a Comment