I am trying to replace a string in a buffer. I have code that will replace the string I'm looking for once. However, I need it to to replace every occurrence of the string. I'm not sure why my code is not replacing all of the occurrences of the string I'm looking for. The string I'm looking for in that file is and the code is below. Thank you.
// print the content of a text file.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main () {
// This is where we'll put the stuff we read from file
char buffer[ 50000 ];
// Fill the buffer with zeros (char strings must be null-terminated)
fill_n( buffer, 50000, '\0' );
ifstream infile;
infile.open ("myfile.txt", ifstream::in);
ofstream fFile;
fFile.open("myfile3.txt", ofstream::out);
while (infile.good())
{
// Read as many as 100 bytes (chars) from the file and stick them in our array
infile.read( buffer, 50000);
// Convert that char array into a STL string, and show the user what we got.
string g( buffer );
string str2="fr3y.txt";
string str="fr3y1.txt";
g.replace(g.find(str2), str2.length(), str);
cout << g << " successfully read from file.\n";
fFile << g << endl;
infile.close();
fFile.close();
}
return 0;
}
1. I need to replace str2.length() with str.length()
2. Instead of using infile.read(), I should use infile.getline()
3. I need to write: string g(buffer, 50000)
After following your suggestion as well as referencing the getline() command from this website, a problem is still occurring. Apparently, the program is terminating abnormally, can you help me with this matter? The code is below:
// print the content of a text file.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main () {
// This is where we'll put the stuff we read from file
char buffer[ 50000 ];
// Fill the buffer with zeros (char strings must be null-terminated)
fill_n( buffer, 50000, '\0' );
ifstream infile;
infile.open ("myfile.txt", ifstream::in);
ofstream fFile;
fFile.open("myfile3.txt", ofstream::out);
while (infile.good())
{
// Read as many as 100 bytes (chars) from the file and stick them in our array
//infile.read( buffer, 50000);
infile.getline(buffer, 50000);
// Convert that char array into a STL string, and show the user what we got.
string g( buffer, 50000 );
string str2="fr3y.txt";
string str="fr3y1.txt";
g.replace(g.find(str2), str2.length(), str);
cout << buffer << " successfully read from file.\n";
fFile << buffer << endl;
infile.close();
fFile.close();
}
return 0;
}
You shouldn't be using character arrays. There is really no need for them.
You can't do a .replace on a string the way you have if the find returns -1
Your closing your files after every loop.
Thank you for the help I really appreciate it, however I'm still having a problem getting the program to run this is what i changed in your code to gel with the input text. However, the program terminates abnormally
It's not complete, but you get the idea of what your trying to do I hope.\
Key points are not using char arrays, and checking before you try to replace.
Okay, this is all that I want my program to do. It runs, but it is getting hung up somewhere and is not writing the output files. I think it is due to an infinite loop, how would I fix this: