Question: Complete the C++ code for the following replaceInFile function. Its purpose is to copy an existing file, replacing all occurrences of one string with another

Complete the C++ code for the following replaceInFile function. Its purpose is to copy an existing file, replacing all occurrences of one string with another to the new file. It uses the find function and replace function from the string library:

// Returns index where str2 is found in str1, or -1 str1.find (string str2, int pos) // Replaces n chars in str1, starting at pos, with str2 str1.replace (int pos, int n, string str2)

The main program will prompt the user for the info prior to calling the function:

void replaceInFile(string oldfile, string newfile, string oldstr, string newstr) { ifstream infile; infile.open ( ); //open input file ofstream outfile; outfile.open ( ); //open output file string line; // loop until the input file variable becomes null while (infile) { // get a line from the input file getline( , line); if (infile) // if not end of file { for (int i = 0; i = 0) { line.replace (position, oldstr. , ); } } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!