School-Coding-Cpp/sfusi/countChar_textfile.cpp

34 lines
508 B
C++

#include <iostream>
using namespace std;
int countChar(FILE * file);
int main(void) {
FILE * file = fopen("file.txt", "rt");
if (file != NULL) {
int countChr = 0;
countChr = countChar(file);
cout << "Number of character: " << countChr << endl;
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;
}