Oct 18, 2011

C Program to count number of characters in the file

C Program to count number of characters in the file. In this program you can learn c file operations. Here we counting the characters by reading the characters in the file one by one and if read character was not an '\n' ,'\t' or EOF, it increments the counter by one. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>

void main()
{
 char ch;
 int count=0;
 FILE *fptr;
 clrscr();
 fptr=fopen("text.txt","w");
 if(fptr==NULL)
 {
  printf("File can't be created\a");
  getch();
  exit(0);
 }
 printf("Enter some text and press enter key:\n");
 while((ch=getche())!='\r')
 {
  fputc(ch,fptr);
 }
 fclose(fptr);
 fptr=fopen("text.txt","r");
 printf("\nContents of the File is:");
 while((ch=fgetc(fptr))!=EOF)
 {
  count++;
  printf("%c",ch);
 }
 fclose(fptr);
 printf("\nThe number of characters present in file is: %d",count);
 getch();
}

Read more Similar C Programs
C Basic

C Strings

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!