I want to read a text file in c++ using ifstream
to know the number of words, characters, lines.
unsigned int wordNum = 0;
unsigned int lineNum = 0;
unsigned int charNum = 0;
char check;
ifstream in("example_2_4.txt");
char temp[30];
if (!in.is_open()) {
cout << "File opening error!" << endl;
}
while (!in.eof()){
in.getline(temp, 30);
wordNum += countWord(temp);
charNum += countChar(temp);
lineNum++;
in.clear();
}
The problem is that eof()
does not work since there exists a line that exceeds 30 characters.
I've changed !in.eof()
to in>>check
and it works well but it reads a character so I can't count all characters in line.
I shouldn't use string
class and can't change buffer size.
Is there any proper way to check eof
?