Write a C Program to count number of characters in a file

Count number of characters in a file


Write a C Program to count number of characters in a file using File Handling. Here’s simple Program to count number of characters in a file using File Handling in C Programming Language.


Below is the source code for C Program to count number of characters in a file using File Handling which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C Program to count number of characters in a file using File Handling  */

/* -----------------file4.txt contains text-------------

  Perpetual sincerity out suspected necessary
  one but provision satisfied. Respect nothing
  use set waiting pursuit nay you looking.
  If on prevailed concluded ye abilities.
  Address say you new but minuter greater.
  Do denied agreed in innate. Can and
  middletons thoroughly themselves him.
  Tolerably sportsmen belonging in september
  no am immediate newspaper. Theirs expect
  dinner it pretty indeed having no of.
  Principle september she conveying did
  eat may extensive.

-------------------------------------------------------*/


#include<stdio.h>

int main()
{
     FILE *fp;
     char a[100];
     long cnt=0;
     int c;

     printf("Type a file name : ");
     gets(a);

     if((fp=fopen(a,"r"))==NULL)
      printf("\nFile dosen't exist.");
     else
     {
      while(1)
      {
       c=fgetc(fp);
       if(feof(fp))
        break;
       cnt++;
      }
      printf("\nFile have %ld characters. \n",cnt);
     }
     fclose(fp);
     return 0;
}


OUTPUT : :


/*  C Program to count number of characters in a file using File Handling  */


Type a file name : C:\\Users\\acer\\Documents\\file4.txt

File have 486 characters.

Process returned 0

Above is the source code for C Program to count number of characters in a file using File Handling which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

Leave a Reply