| | |
Read from text, need help!!!!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2009
Posts: 10
Reputation:
Solved Threads: 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
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
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
>> How would i tell when the file ends
when the loop below exits
C++ Syntax (Toggle Plain Text)
std::string word; ifstream in("file.txt"); while( in >> word) 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.
•
•
Join Date: Dec 2007
Posts: 360
Reputation:
Solved Threads: 69
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 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
Please use code tags - Please mark solved threads as solved
![]() |
Other Threads in the C++ Forum
- Previous Thread: Compaitibility for Visual Studio 2008
- Next Thread: How can I combine this one? (TURBO C++)
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






