/* Program to illustrate string processing using a global file reference - more modular*/ #include #include #include using namespace std; //function prototypes void change(string &line); bool getchanges(ifstream &changesfile, string &oldstr, string &newstr); void change_one_line(string &line, string oldstr, string newstr); //global declarations ifstream changesfile; int main() { string line; // open input file ifstream infile("c:\\bc\\CISC1110\\pgms\\chapter8\\myinput.txt"); //comment-out for debugging //ifstream infile("con"); //un-comment for debugging // open output file //ofstream outfile("c:\\bc\\CISC1110\\pgms\\chapter8\\myoutput.txt"); //comment-out for debugging ofstream outfile("con"); //un-comment for debugging while (getline(infile, line)) { outfile << line << endl; //output old line change(line); //make changes outfile << line << endl; //output revised line } infile.close(); //close input file outfile.close(); //close output file // system("pause"); return 0; } /* Function change() * Input: * line - a reference to the string to be processed * Globals: * changesfile - global reference to the file that contains the replacement pairs * Process: * repeatedly call getchanges() to get a new set of * replacement strings (oldstr & newstr), until there * are no more sets of changes. * Output: * line - the processed string */ void change(string &line) { string oldstr, newstr; // open changes file changesfile.open("c:\\bc\\CISC1110\\pgms\\chapter8\\mychanges.txt"); //comment-out for debugging //changesfile.open("con"); //un-comment for debugging changesfile.clear(); //reset EOF flag while (getchanges(changesfile, oldstr, newstr)) { change_one_line(line, oldstr, newstr); } changesfile.close(); return; } /* Function getchanges(): * Input: * changesfile - a reference to the file that contains the replacement pairs * oldstr - a reference to the old string to search for * newstr - a reference to the new replacement string * Process: * reads in a pair of strings (oldstr & newstr) if they exist * Output: * oldstr - the old string to search for * newstr - the new replacement string * if they exist, returns true; else, returns false */ bool getchanges(ifstream &changesfile, string &oldstr, string &newstr) { if (getline(changesfile, oldstr)) //read oldstr if it exists { getline(changesfile, newstr); //read newstr return true; } return false; //reached end-of-file } /* Function change_one_line(): * Input: * line - a reference to the string to be processed * oldstr - the old string to search for * newstr - the new replacement string * Process: * searches line for position of oldstr * if oldstr exists, replace oldstr with newstr * returns when all replacements have been made * Output: * line - the processed string */ void change_one_line(string &line, string oldstr, string newstr) { string::size_type pos; pos = line.find(oldstr, 0); //search for oldstr while (pos != line.npos) //if oldstr found { line.replace(pos, oldstr.length(), newstr); //replace oldstr with newstr pos = line.find(oldstr, pos+newstr.length()); //search for oldstr } return; }