From: Luigi S. <gi...@li...> - 2001-07-06 04:54:57
|
At 22.51 05/07/01 +0000, you wrote: >hi everyone, i hope someone can help me. at the moment i am writing a >program, heres what i want it to do: > >i want it to open a text document that i have created and i want it to >search for '$$$$$' (after this there is some text i want). i then want it >to read the text that follows. >... >_________________________________________________________________________ Ian - I hope that the following example code is of help test.cpp --------- // test the look_for function #include <fstream.h> #include "lookfor.h" int main(int argc, char **argv) { if (argc != 3) cerr << "usage: " << *argv << " file key\n"; else { const char *fname = argv[1]; ifstream file(fname); // open the text file string key = argv[2]; cout << "key is : \"" << key << "\"\n"; if (look_for(file, key)) // load the value { string val; file >> val; cout << "value is : \"" << val << "\"\n"; } else cout << "sorry key not found in \"" << fname << "\"\n"; } return 0; } lookfor.h ------------ #ifndef lookfor_h #define lookfor_h #include <iostream> #include <string> // look_for = look for the key on the stream is, return true if found bool look_for(istream &is, const string &key); #endif lookfor.cpp --------------- #include "lookfor.h" bool look_for(istream &is, const string &key) { bool found = false; string s; while (!found && is >> s) found = (s == key); return found; } here is the test data test.dat ---------- file to be used as input for "test" key_1: $$$$$ value_1 key_2: &&&&& value_2 ... try the following commands: test test.dat $$$$$ test test.dat $$$$ --- gisan |