passing fstream to a function and searching for a string

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

Join Date: Nov 2008
Posts: 20
Reputation: jimbob90 is an unknown quantity at this point 
Solved Threads: 0
jimbob90 jimbob90 is offline Offline
Newbie Poster

passing fstream to a function and searching for a string

 
0
  #1
Dec 9th, 2008
read in MYDATA (with a space) from cin and find MYDATA in the file, print the line if its there. The line is going to be MYDATA + string.
  1. int main (){
  2. fstream filename;
  3. filename.open(av[1]);
  4. string name;
  5. getline (cin, name);
  6. lookeyLookey (name, filename);
  7. }
  8. void lookeyLookey (string& name, ofstream& filename){
  9. //how to look for MYDATA in file I have no idea
  10. if (found)
  11. cout << name << STRING AFTER MYDATA;
  12.  
  13. else
  14. cout << name << ": not found";
  15. }
Last edited by jimbob90; Dec 9th, 2008 at 9:08 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
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: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: passing fstream to a function and searching for a string

 
0
  #2
Dec 9th, 2008
you need to put getline() in a loop inside the function lookyLooky() so that it reads all the lines in the file. Then on each loop iteration use the std::string's find() method to search for the desired text.
  1. while( getline(filename, line) )
  2. {
  3. // check to see if this line contains the desired string
  4. }
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 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