0

Hi guys,

I know that there might already be a tread about reading from file but this has a certain hurdle to jump...

I have to read in a 1000 word document into something that will make a list of unique words and how many times they appear. Thats not to bad, but the exception and hard part for me is that each word is seperated by 1 or MORE spaces, which is where i have the problem. If it was only one, and only one, space, i would be a normal read in but the fact that the text file could have "This could be a sentence" would through me off. so my question i need help with is how to read in indiviual words and skip white spaces, even if there are 30 white spaces in a row.

-Basketball4567
P.S
How would i tell when the file ends


Thanks guys

3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by jencas
0

What is the problem? Just use ifstream's >> operator and it will skip all white space (spaces, tags, etc) between words.

>> How would i tell when the file ends

when the loop below exits

std::string word;
ifstream in("file.txt");
while( in >> word)
   cout << word << "\n";

Edited by Ancient Dragon: n/a

0

And use a std::map<std::string, int> to count the occurences of the words in the file:
- if you cannot find the word in the map, then create a new map entry for that word with the int set to 1
- if you fin the word in the map, increment the int by 1

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.