| | |
passing fstream to a function and searching for a string
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
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 c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






