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.
- Declare the variables.
- Using control statements.
- 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);
}