/* string processing program - less modular */ #include #include #include using namespace std; //function prototypes void change(string &line); 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"); //un-comment when using DevC++ return 0; } /* Function change() * Input: * line - a reference to the string to be processed * 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; string::size_type pos; // open changes file ifstream changesfile("c:\\bc\\CISC1110\\pgms\\chapter8\\mychanges.txt"); //comment-out for debugging //ifstream changesfile("con"); //un-comment for debugging while (getline(changesfile, oldstr)) //read oldstr { getline(changesfile, newstr); //read newstr 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 } } changesfile.close(); return; }