by Dinesh Thakur Category: C Programming (Pratical)

In this program user ask to print out the char of a file along with their positions. As user declare the variables for containing the value in it. Here user pointer type variable declaring then ask to enter the file name. if condition will checks out the has file exist or not if the file not exist the message will be file cant open if it exist the while condition will checks out all the character to EOF. With the method of display all the characters will be counted as output.

Problem Statement:

This is C program to count the character in a file.

  1. Declare the variables.
  2. Using control statements.
  3. Display output as result.

Here is C source code counting the character in a file. The output of this program shown below. 

#include<stdio.h>
void main()
{
      FILE *fp;
      char ch,fname[20];
      int n;
      printf("Enter file Name :");
      gets(fname);
      fp=fopen(fname,"r");
      if(fp==NULL)
        {
              printf("file can't be opened");
             exit(0);
        }
             fseek(fp,0,0);
             ch=fgetc(fp);
             while(ch!=EOF)
                     {
                           n=ftell(fp);
                           printf("%c ",ch);
                           printf("%d \n",n);
                           ch=fgetc(fp);
                    }
                           fclose(fp);
}

Char of a File along with their Positions

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Search Content