Hello,
I am trying to teach myself C++ and am currently studying file I/O. What I am trying to do is this (and I am sure you have heard this before): open a text file and output each word with the number of occurrences. For example, if the file contains the phrase "the word the is used twice in this sentence", the output would look like this:
WORD QTY
the 2
word 1
is 1
etc, etc...
Now, finding the number of words is simply a matter of counting the whitespaces and adding 1.
int main()
{
string input, fileName;
cout << "Please enter filename: ";
cin >> input;
cout << endl;
ifstream inClientFile;
fileName = input;
inClientFile.open(fileName.c_str());
if (!inClientFile)
{
cerr << "File could not be opened!" << endl;
exit( 1 );
}
char ch; int words=0;
while(!inClientFile.eof())
{
inClientFile.get(ch);
if (isspace(ch))
words++;
cout << ch;
}
cout << endl;
words++;
cout << "There are " << words << " words in this sentence.";
inClientFile.close();
return 0;
}
Obviously, a whitespace signifies that the characters before it, and after the last whitespace, are a word. But how do you pull out what is between the whitespaces? What is the logic here? Thanks...
l0g0rrhea