Thursday, July 21, 2011

Program to count no of characters, blank spaces, lines in a file

/*Serial No.104     [swami81.cpp]*/

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
int i;
char ch,c=0,nl=1,name[20],bs=0;
clrscr();
FILE *fp1;
printf("Enter the name of file=%s\n",name);
gets(name);       //name is FILE NAME.
fp1=fopen(name,"r");
if(fp1==NULL)
    {
    printf("file doesn't exist");
    getch();
    fclose(fp1);
    exit;
    }
printf("file data are as follows:\n\n");
while(1)
    {
    ch=fgetc(fp1);
    if(ch==EOF)
       break;
    else
       c++;          // c is no. of characters.
       if(ch=='\n')
         {
         nl++;       // nl is no. of lines.
         }
       if(ch==' ')
         {
         bs++;       // bs is no. of blank spaces.
         }
    }
printf("No of characters in file=%d\n\n",c);
printf("No of blank spaces in file=%d\n\n",bs-1);
printf("No of characters in file includes blank spaces,tabs,new line characters,\nevery charater\n\n");
printf("No of lines in file=%d\n",nl);
fclose(fp1);
getch();
}

1 comment:

  1. this is very easy for understanding.thank you so much.

    ReplyDelete

If you have any doubt, feel free to ask...