| | |
passing fstream to a function and searching for a string
![]() |
•
•
Join Date: Nov 2008
Posts: 20
Reputation:
Solved Threads: 0
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.
c++ Syntax (Toggle Plain Text)
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"; }
Last edited by jimbob90; Dec 9th, 2008 at 9:08 pm.
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.
C++ Syntax (Toggle Plain Text)
while( getline(filename, line) ) { // check to see if this line contains the desired string }
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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Determining the number of unique words in a .txt file
- Next Thread: HELP! It doesn't work :(
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ char class classes code coding compaitibility compile console conversion count delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error factorial file floatingpoint forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple net news node number numbertoword output parameter payment pointer problem program programming project projectassignmenthelp protection python random rank read recursion reference rpg skills string strings temperature template test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






