School-Coding-Cpp/sfusi/countLine_textfile.cpp

36 lines
562 B
C++

#include <iostream>
#define SIZE_LINE 1000+1
using namespace std;
int countLine(FILE * file);
int main(void) {
FILE * file = fopen("file.txt", "rt");
if (file != NULL) {
int countRow = 0;
countRow = countLine(file);
cout << "Number of character: " << countRow << endl;
fclose(file);
} else {
perror("Error (source)");
}
return 0;
}
int countLine(FILE * file) {
int countRow = 0;
char line[SIZE_LINE];
while (fgets(line, sizeof(line), file) != NULL) {
countRow++;
}
return countRow;
}