c program to count number of lines in a file, Number of lines in given text file
#include
void main()
{
FILE *fopen(), *fp;
int c , nc, nlines;
char filename[40] ;
nlines = 0 ;
nc = 0;
printf("Enter the file name: ");
gets( filename );
fp = fopen( filename, "r" );
if ( fp == NULL )
{
printf("Sorry Cannot open %s for reading \n", filename );
exit(1); /* End program */
}
c = getc( fp ) ;
while ( c != EOF )
{
if ( c == '\n' )
nlines++ ;
nc++ ;
c = getc ( fp );
}
fclose( fp );
if ( nc != 0 )
{
printf("There are %d characters in %s \n", nc, filename );
printf("There are %d lines \n", nlines );
}
else
{
printf("Ohh.h....File: %s is empty \n", filename );
}
}
No comments:
Post a Comment