In this post we will write a C++ program to count the number of lines in a file.
C++ program to count the number of lines in a file.
#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
int main()
{
system("cls");
int l=0;
ofstream a("sample.txt",ios::out);
a<<"If you think education is expensive then try ignorance"<<std::endl;
a<<"Migh is Sight"<<std::endl;
a<<"we are here to play"<<std::endl;
a<<"now we are you";
a.close();
char str[150];
ifstream b("sample.txt",ios::in);
while(!b.eof())
{
l++;
b.getline(str,150);
}
std::cout<<"The number of lines in flie = "<<l;
getch();
return 0;
}
Output
The number of lines in file = 4