programming-examples/c/Structures_and_unions/C Program to count number of characters in the file.c

28 lines
690 B
C
Raw Normal View History

2019-11-15 12:59:38 +01:00
#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();
}