You can pass streams around by reference relatively freely, however this may not be quite what you want. If you want to search in a stream/file for a particular string you could do something like this
Code:
std::string search_line(const std::string &pat, std::istream &is) {
std::string tok;
while(is >> tok) {
if(tok == pat) {
std::string tail;
std::getline(is,tail);
return tok + tail;
}
}
return "";
}
....
std::ifstream ifs("file.txt");
std::string line = search_line("joe",ifs);
if(!line.empty()) std::cout << "Found " << line << std::endl;
This will advance through a stream/file until a string exactly matches pat, then return that line from the token to the end of line.