Problem
You have to count the numbers of characters, words, and linesor some other type of text elementin a text file.
Solution
Use an input stream to read the characters in, one at a time, and increment local statistics as you encounter characters, words, and line breaks. Example 4-26 contains the function countStuff, which does exactly that.
Example 4-26. Calculating statistics about a text file
#include
#include
#include
#include
using namespace std;
void countStuff(istream& in,
int& chars,
int& words,
int& lines) {
char cur = '