Saturday, March 3, 2012

PROGRAM TO COUNT NUMBER OF LINES IN A FILE


/*******************(soeasyprograms.blogspot.in)***********************/
/*To count the number of lines in a file */

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<fstream.h>
void main()
{

int i,n,tlines=0;
char str[20],ch;
clrscr();
fstream f;
f.open("manik.txt",ios::out);
cout<<" ****** Enter the contents of file ******: ";
cin.getline(str,100);
f<<str;
f.close();
f.open("manik.txt",ios::in);
while(f)
{
f.get(ch);
if(ch==' '||ch==EOF)
{
    tlines++;
    if(ch==EOF)
    {
 tlines++;
    }
}

}
cout<<"\n There are "<<tlines<<" lines in the entered file.";
getch();
}


/*******************(soeasyprograms.blogspot.in)***********************/

No comments:

Post a Comment