DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   passing fstream to a function and searching for a string (http://www.daniweb.com/forums/thread161536.html)

jimbob90 Dec 9th, 2008 9:04 pm
passing fstream to a function and searching for a string
 
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.
int main (){
fstream filename;
filename.open(av[1]);
string name;
getline (cin, name);
lookeyLookey (name, filename);
}
void lookeyLookey (string& name, ofstream& filename){
    //how to look for MYDATA in file I have no idea
    if (found)
        cout << name << STRING AFTER MYDATA;

  else
        cout << name << ": not found";
}

Ancient Dragon Dec 9th, 2008 9:44 pm
Re: passing fstream to a function and searching for a string
 
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.
while( getline(filename, line) )
{
    // check to see if this line contains the desired string
}


All times are GMT -4. The time now is 3:33 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC