School-Coding-Cpp/sfusi/countChar_textfile.c

32 lines
477 B
C

#include <stdio.h>
int countChar(FILE * file);
int main(void) {
FILE * file = fopen("file.txt", "rt");
if (file != NULL) {
int countChr = 0;
countChr = countChar(file);
printf("Number of character: %d\n", countChr);
fclose(file);
} else {
perror("Error (source)");
}
return 0;
}
int countChar(FILE * file) {
int countChr = 0;
int chr;
while ((chr = fgetc(file)) != EOF) {
countChr++;
}
return countChr;
}