- Back to Home »
- FILE HANDLING »
- c Program to count characters in given file, Count characters in from file, number of characters in given file
c Program to count characters in given file, Count characters in from file, number of characters in given file
Posted by : ANIMESH SHAW
Monday, 6 February 2012
#include<stdio.h>
void main()
{
FILE *fopen(), *fp;
int c , nc, nlines;
char filename[40] ;
nlines = 0 ;
nc = 0;
printf("Enter file name: ");
gets( filename );
fp = fopen( filename, "r" );
if ( fp == NULL )
{
printf("Cannot open %s for reading \n", filename );
exit(1); /* terminate 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("File: %s is empty \n", filename );
}
}

Post a Comment