下面是尝试替换文本文件 parameters.in 中的两个字符串的代码,但是由于某种原因,只有其中的第二个被替换。使用函数 ModifyParametersIn 在 main() 中进行替换。任何人都可以提示如何解决这个问题吗?
void ModifyParametersIn(string search_string, string replace_string) {
string inbuf;
fstream input_file("parameters.in", ios::in);
ofstream output_file("parameters.out");
while (!input_file.eof()) {
getline(input_file, inbuf);
int spot = inbuf.find(search_string);
if(spot >= 0) {
string tmpstring = inbuf.substr(0,spot);
tmpstring += replace_string;
tmpstring += inbuf.substr(spot+search_string.length(), inbuf.length());
inbuf = tmpstring;
}
output_file << inbuf << endl;
int main() {
...
string search_string = "start_a0 = " + str_start_a0;
string replace_string ="start_a0 = " + str_middle_a0;
cout << search_string << endl;
ModifyParametersIn(search_string, replace_string);
search_string = "start_tanb = " + str_start_tanb;
replace_string = "start_tanb = " + str_middle_tanb;
ModifyParametersIn(search_string, replace_string);
cout << search_string<< endl;
return 0;
}