Searching for words in a file

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2007
Posts: 12
Reputation: sarahger9 is an unknown quantity at this point 
Solved Threads: 0
sarahger9 sarahger9 is offline Offline
Newbie Poster

Searching for words in a file

 
0
  #1
Oct 4th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,585
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: 1487
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Searching for words in a file

 
0
  #2
Oct 4th, 2007
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
  1. std::vector<std::string> theArray;
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: Oct 2007
Posts: 12
Reputation: sarahger9 is an unknown quantity at this point 
Solved Threads: 0
sarahger9 sarahger9 is offline Offline
Newbie Poster

Re: Searching for words in a file

 
0
  #3
Oct 4th, 2007
This is what I have so far, like I said, basically all it does is open the files and counts the words.
  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,585
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: 1487
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Searching for words in a file

 
0
  #4
Oct 4th, 2007
Originally Posted by sarahger9 View Post
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.
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  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC