Read from text, need help!!!!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2009
Posts: 10
Reputation: basketball4567 is an unknown quantity at this point 
Solved Threads: 0
basketball4567 basketball4567 is offline Offline
Newbie Poster

Read from text, need help!!!!

 
0
  #1
Sep 5th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,502
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1479
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

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

 
-7
  #2
Sep 6th, 2009
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
  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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 360
Reputation: jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice 
Solved Threads: 69
jencas jencas is offline Offline
Posting Whiz

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

 
0
  #3
Sep 7th, 2009
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
If you are forced to reinvent the wheel at least try to invent a better one!

Please use code tags - Please mark solved threads as solved
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC