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.

How would i tell when the file ends

Thanks guys

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";
Member Avatar for jencas

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.