943,107 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 942
  • C++ RSS
Feb 7th, 2010
0

Searching through text files for individual words

Expand Post »
Hi,

Can anyone help me with this problem.For example i have a text file called food.txt with contents


1 apples
2 oranges
3 mango
4 bananna


What i would like to do is enter the choice of fruit into a string and loop through the file until the word is found.My problem being i dont fully understand any of the examples online and would be grateful if someone could point me in the right direction.

Here is my code so far

C++ Syntax (Toggle Plain Text)
  1. main(){
  2.  
  3. char filename[size];
  4. char line[size];
  5. string fruit;
  6.  
  7.  
  8. cout << "Please enter filename: ";
  9. cin >> filename;
  10.  
  11. cout << "enter fruit:" << endl;
  12. cin >> fruit;
  13. cout << fruit;
  14.  
  15.  
  16. cout << "\n Filename is: " << filename << endl;
  17.  
  18. cout << "Opening file.." << endl;
  19.  
  20. fstream* file = new fstream(filename,fstream::in);
  21.  
  22. while(file->getline(line,size) != NULL)
  23. {
  24. cout << line;
  25.  
  26. // if the line is the same as fruit entered print out the line(this does not work!)
  27. if(line == fruit)
  28. {
  29. cout << line;
  30. }
  31. }
  32.  
  33. system("PAUSE");
  34.  
  35.  
  36. }
Last edited by StaticX; Feb 7th, 2010 at 12:29 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
StaticX is offline Offline
71 posts
since Oct 2009
Feb 7th, 2010
0
Re: Searching through text files for individual words
First of all you don't want to use getline() to read a single word because getline() will read the entire line. It is better to use >> operator to read words like this:
C++ Syntax (Toggle Plain Text)
  1. std::string word;
  2. std::string filename;
  3. ...
  4. ...
  5. // open the ifstream (Note: ifstream should NOT be declared as a pointer!)
  6. // use ifstream instead of fstream for input-only streams
  7. ifstream file(filename.c_str());
  8. // read the file one word at a time
  9. while( file >> word )
  10. {
  11. if( word == fruit )
  12. {
  13. cout << "Found it!\n";
  14. break;
  15. }
  16. }
Last edited by Ancient Dragon; Feb 7th, 2010 at 12:40 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2280
Retired and Enjoying Life
Ancient Dragon is online now Online
21,936 posts
since Aug 2005
Feb 7th, 2010
0
Re: Searching through text files for individual words
Thats great thank you for the help,what i forgot to mention was that when the word is found,i want to take it out of the file.


For example in my file:

1 apples
2 oranges
3 mango
4 bananna

There is numbers and a space before each of the fruits.So if i picked #2,there is a number and a whitespace character preceding it.

What im trying to figure is how to take the contents of the line in the file out,but am struggling to come to terms with how to do so.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
StaticX is offline Offline
71 posts
since Oct 2009
Feb 7th, 2010
0
Re: Searching through text files for individual words
The only way that can be done is to rewrite the entire file. One way is to read everything into an array, or vector, or strings, then reopen the file for writing. Rewrite all the strings except the one you want to delete.

Since the lines contain other stuff you might want to use getline() as you originally posted. Then replace if(line == fruit) with this: if( line.find(fruit) != string::npos)

>> while(file->getline(line,size) != NULL)
Recode that like this: while( getline(file,line) ) if you make line a std::string instead of char*
Last edited by Ancient Dragon; Feb 7th, 2010 at 2:39 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2280
Retired and Enjoying Life
Ancient Dragon is online now Online
21,936 posts
since Aug 2005
Feb 7th, 2010
0
Re: Searching through text files for individual words
:/ im sorry again but my communication today is dire,iv been looking at a computer screen for most of the day so my head is abit clouded.

What i MEANT to say(and this time im going to explain it properly!) is i dont mean literally TAKE OUT of the file,just to display them on the screen again.

FOr instance if the user wishes to see what fruit is #3,they input this number and it is retrieved,or alternativley they can enter the name of the fruit and it is displayed.

Im sorry for not being so clear the last time!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
StaticX is offline Offline
71 posts
since Oct 2009
Feb 7th, 2010
0
Re: Searching through text files for individual words
Well, with the help above, I'm certain you can figure it out. You know the format of the file, you know what you type in at the prompt. From there, AD's code has a bunch of what you need. Just add to that what you already know.
Moderator
Reputation Points: 3275
Solved Threads: 886
Posting Sage
WaltP is offline Offline
7,699 posts
since May 2006
Feb 7th, 2010
0
Re: Searching through text files for individual words
Unfortunately thats not the case walt.Im very confused about searching text files by individual lines and starting at a point in a text file through a key word,id appreciate any help thats the reason im posting on this board.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
StaticX is offline Offline
71 posts
since Oct 2009
Feb 7th, 2010
0
Re: Searching through text files for individual words
Read a line. Check if the line contains the number (at the beginning) or the text (at the end). Since you're using character strings, the strxxx family of functions will help. Also, you can look at each individual character in the array/string.

Also, in Englis, there is a space after each period, comma, question mark, etc. They are there for readability, not just to annoy the writer
Moderator
Reputation Points: 3275
Solved Threads: 886
Posting Sage
WaltP is offline Offline
7,699 posts
since May 2006
Feb 7th, 2010
0
Re: Searching through text files for individual words
There are any number of ways to do this.

If I assume the file has a number of lines of text and each line has two fields, one being a word which is preceded by a space which is preceded by a number, then I would create a class called Item. Each object of type item will have a number and a name as member variables. I can read file using getline() and separate the two fields after reading in the line, or I can use >> to read in each field of the line one by one. Either way I will use a loop to read the file line by line putting the value of each field into an object of type Item. I will then push Item onto the back of a vector of Items and go on to the next line, unil I've read the whole file into the vector of Items. Then I can search the vector of Items by using either of the member variables of the items as the target.

Alternatively I could use a map to store the information in the file and use the number as the key to the map and the name as the data.

If I wasn't allowed to use vectors or maps and I didn't know about classes then I would use parallel arrays and use the index of the number or the name to get the other information about a given item.
Last edited by Lerner; Feb 7th, 2010 at 4:56 pm.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005

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: Plume Boundary
Next Thread in C++ Forum Timeline: Standard Deviation





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


Follow us on Twitter


© 2011 DaniWeb® LLC