943,626 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 403
  • C++ RSS
Sep 5th, 2009
0

Read from text, need help!!!!

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
basketball4567 is offline Offline
12 posts
since Aug 2009
Sep 6th, 2009
-7

Re: Read from text, need help!!!!

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
C++ Syntax (Toggle Plain Text)
  1. std::string word;
  2. ifstream in("file.txt");
  3. while( in >> word)
  4. cout << word << "\n";
Last edited by Ancient Dragon; Sep 6th, 2009 at 12:03 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Sep 7th, 2009
0

Re: Read from text, need help!!!!

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
Reputation Points: 395
Solved Threads: 71
Posting Whiz
jencas is offline Offline
362 posts
since Dec 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Compaitibility for Visual Studio 2008
Next Thread in C++ Forum Timeline: How can I combine this one? (TURBO C++)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC