I haven't ever tried to apply the operator >> on a deferenced pointer to ifstream class object.
Try to see if passing by reference works. Also, by outputting the read text, check whether the strings are being read correctly. To not end-up in an infinite loop, apply a limitting condition as follows, just during the debugging.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int countTextWords(ifstream& file)
{
string textWord;
int wordCount = 0;
unsignedint limit = 1000;
while ( ( file >> textWord ) && (limit--) )
{
wordCount++;
}
return wordCount;
}
// and in your main()
numWords = countTextWords( textFile );
@TC: It does seem odd that it isn't working, as it looks like your code is fine. I'd try debugging it and temporarily storing the input data as a string to see what's actually being read.
Ahh I see. Coder777 you were right, I couldn't read anymore input through that ifstream object because the failbit was set after reading characters. Therefore I needed to clear the failbit before reading more input as ne555 pointed out.