944,038 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3484
  • C++ RSS
Oct 4th, 2007
0

Searching for words in a file

Expand Post »
Hi,
I'm a beginner, so please forgive my inadequate knowledge.
I'm am trying to write a program that searches one file for specific words and their replacements found in another file. As of yet, all I have is a program that opens my files and counts the words in them. I think I need to put the words in a string array in order to search through them, but I don't know how to do this. If someone could help get me started on this part, I can play around with the rest of the program and hopefully have some luck.
Thanks
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sarahger9 is offline Offline
12 posts
since Oct 2007
Oct 4th, 2007
0

Re: Searching for words in a file

show us the code you have written so far. Do you know how to use vector class? That is the easiest array to use. You can make a vector of string objects
C++ Syntax (Toggle Plain Text)
  1. std::vector<std::string> theArray;
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,954 posts
since Aug 2005
Oct 4th, 2007
0

Re: Searching for words in a file

This is what I have so far, like I said, basically all it does is open the files and counts the words.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. int main () {
  7.  
  8. ifstream potterfile, keywordfile;
  9. ofstream corrected;
  10. string word, text, file[300];
  11. int count;
  12.  
  13. count = 0;
  14.  
  15. //Open Potter text file, check if open
  16. potterfile.open("potter.txt");
  17. if (!potterfile.is_open()){
  18. cout << "Error, could not open the file" << endl;
  19. return -1;
  20. }
  21. //Count words in Potter text file
  22. potterfile>>word;
  23. while (!potterfile.eof()){
  24. count = count + 1;
  25. potterfile >> word;
  26. }
  27. cout <<"The number of words in the file is:" << count << endl;
  28.  
  29.  
  30.  
  31. //Open Keyword file check if open
  32. keywordfile.open("keyword.txt");
  33. if (!keywordfile.is_open()){
  34. cout << "Error, could not open the file" << endl;
  35. return -1;
  36. }
  37.  
  38.  
  39. return 0;
  40.  
  41. }
I am not familiar with the vector class string, but if you could elaborate on it a little I may be able to figure it out.
Thanks
Last edited by Ancient Dragon; Oct 4th, 2007 at 8:21 pm. Reason: add code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sarahger9 is offline Offline
12 posts
since Oct 2007
Oct 4th, 2007
0

Re: Searching for words in a file

Click to Expand / Collapse  Quote originally posted by sarahger9 ...
I am not familiar with the vector class string, but if you could elaborate on it a little I may be able to figure it out.
Thanks
See these code snippets.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,954 posts
since Aug 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: error C2664
Next Thread in C++ Forum Timeline: urgent help postfix evaluation





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


Follow us on Twitter


© 2011 DaniWeb® LLC