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 );   
    }  
 }

Popular Post

Total Pageviews

Sign In

Follow by Email

Powered by Blogger.

Like Us on Facebook

Search This Blog

Loading...

Indian Coder

- Copyright © Programming Mania -Metrominimalist- Powered by Blogger - Designed by Johanes Djogan -