fstream class, not reading word for word

1
2
3
4
5
6
7
8
9
10
int countTextWords(ifstream * file)
{
	string textWord;
	int wordCount = 0;
	while((*file) >> textWord)
	{
		wordCount++;
	}
	return wordCount;
}


for some reason, (*file) >> textWord will not read words into the string. What am I doing wrong?
you write all of them incorrectly i think
replace with this:
 
	while((*file) > textWord)

i think it doesnt work but test hasn't any problem.
If I did that, I would be comparing (*file) with textWord.

What I want is to read a word on the (*file) object onto the string and then count how many words I read.

The reason I have it (*file) >> textWord as the condition is so that if it reaches the EOF then the while loop will end.

However my problem is that when I execute that function; my program simply skips the while((*file) >> textword) loop.

Here is my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

void isFileOpen(ifstream * file);
int countTextChars(ifstream * file);
int countTextWords(ifstream * file);

int main()
{
	//gets file path
	cout << "Text File Path: ";
	string filePath;
	getline(cin, filePath);

	int numCharacters = 0;
	int numWords = 0;

	//reads text file
	ifstream textFile;
	textFile.open(filePath);

	isFileOpen(&textFile); //checks to see if file is open
	numCharacters = countTextChars(&textFile); //Counts # of Characters in Text File
	textFile.seekg(0, ios::beg); //resets file to beginning
	numWords = countTextWords(&textFile); //Counts # of Words in Text File
	isFileOpen(&textFile); //Outputs end of file

	cout << numCharacters << " characters read." << endl;
	cout << numWords << " words read." << endl;

	textFile.close();


}

void isFileOpen(ifstream * file)
{
	if ((*file).eof())
	{
		cout << "End of File Reached. " << endl;
	}

	if ((*file).bad())
	{
		cout << "Error opening file: Type Mismatch" << endl;
		exit(EXIT_FAILURE);
	}
	if (!((*file).is_open()))
	{
		cout << "File Failed to Open: Invalid File Path" << endl;
		exit(EXIT_FAILURE);
	}
}

int countTextChars(ifstream * file)
{
	int charCount = 0;
	while((*file).is_open() && (!((*file).eof())))
	{
		char textChar = (*file).get();
		charCount++;
		if (textChar == '\n')
			charCount--;
	}
	return charCount;
}

int countTextWords(ifstream * file)
{
	string textWord;
	int wordCount = 0;
	while((*file) >> textWord)
	{
		wordCount++;
	}
	return wordCount;
}
Last edited on
try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
using namespace std;

void isFileOpen(ifstream * file);
int countTextChars(ifstream * file);
int countTextWords(ifstream * file);

int main()
{
	//gets file path
	cout << "Text File Path: ";
	string filePath;
	getline(cin, filePath);

	int numCharacters = 0;
	int numWords = 0;

	//reads text file
	ifstream textFile;
	textFile.open(filePath, ios_base::in);
	isFileOpen(&textFile); //checks to see if file is open
	numCharacters = countTextChars(&textFile); //Counts # of Characters in Text File
	textFile.seekg(0, ios::beg); //resets file to beginning
	numWords = countTextWords(&textFile); //Counts # of Words in Text File
	isFileOpen(&textFile); //Outputs end of file

	cout << numCharacters << " characters read." << endl;
	cout << numWords << " words read." << endl;

	textFile.close();


}

void isFileOpen(ifstream * file)
{
	if ((*file).eof())
	{
		cout << "End of File Reached. " << endl;
	}

	if ((*file).bad())
	{
		cout << "Error opening file: Type Mismatch" << endl;
		exit(EXIT_FAILURE);
	}
	if (!((*file).is_open()))
	{
		cout << "File Failed to Open: Invalid File Path" << endl;
		exit(EXIT_FAILURE);
	}
}

int countTextChars(ifstream * file)
{
	int charCount = 0;
	while((*file).is_open() && (!((*file).eof())))
	{
		char textChar = (*file).get();
		charCount++;
		if (textChar == '\n')
			charCount--;
	}
	return charCount;
}

int countTextWords(ifstream * file)
{
	string textWord;
	int wordCount = 0;
	while((*file) >> textWord)
	{
		wordCount++;
	}
	return wordCount;
}
The same thing happens.

The problem occurs during the countTextWords(ifstream * file); function.
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;
  unsigned int 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.
if the words are seperated by whitespaces it works.

What makes you think that it doesn't?

[EDIT]: read this:

http://www.cplusplus.com/reference/istream/istream/seekg/?kw=seekg

If the eofbit flag is set before the call, the function fails (sets failbit and returns).
Last edited on
1
2
numCharacters = countTextChars(&textFile);
textFile.clear();
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.

Thank you :)
Topic archived. No new replies allowed.