/* string processing program using a global file reference - less modular */ #include #include #include using namespace std; //function prototypes void change(string &line); //global declarations ifstream changesfile; int main() { string line; // open input file ifstream infile("c:\\bc\\cis1_5\\newpgms\\chapter8\\myinput.txt"); //comment-out for debugging // ifstream infile("con"); //un-comment for debugging // open output file // ofstream outfile("c:\\bc\\cis1_5\\newpgms\\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; string::size_type pos; // open changes file changesfile.open("c:\\bc\\cis1_5\\newpgms\\chapter8\\mychanges.txt"); //comment-out for debugging // changesfile.open("con"); //un-comment for debugging changesfile.clear(); //reset EOF flag 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.erase(pos, oldstr.length()); //erase oldstr line.insert(pos, newstr); //insert newstr pos = line.find(oldstr, pos+newstr.length()); //search for oldstr } } changesfile.close(); return; }