#include<iostream>#include<fstream>#include<cstdlib>#include<string>using namespace std; string replace_all(ifstream infile,string from,string to){ string str; infile>>str; cout<<"The basic string: "<<str<<endl; //replace: for(string::size_type pos(0);pos!=string::npos;pos+=to.length()) { if((pos=str.find(from,pos))!=string::npos) str.replace(pos,from.length(),to); else break; } return str;} int main(){ string s,f,t; ofstream outfile("IOreplace.dat",ios::out); if(!outfile) { cerr<<"open error!"<<endl; exit(1); } cin>>s; outfile<<s; outfile.close(); //infile and replace ifstream infile("IOreplace.dat",ios::in); if(!infile) { cerr<<"open error!"<<endl; exit(1); } cin>>f>>t; cout<<"After replace: "<<replace_all(infile,f,t); infile.close(); return 0;}

评论