C++:替换文本文件中的某些字符

#include
#include
#include


using namespace std;


void main(){
string file_path = "G:\\1234.txt";//文件路径
string out_path = "G:\\12345.txt";//输出路径
string str;
string::size_type pos = 0;
ifstream instream;
ofstream outstream;
instream.open(file_path);
if(!instream)
cout<<"error"< outstream.open(out_path);
while(getline(instream,str)){
pos = str.find("hyu");//查找字符在string中第一次出现的位置
if (pos==string::npos)//判断是否存在“hyu”这个字符
{
cout<<"Can not find this string !"< }else{
str.replace(pos,3,"www");//用www替换hyu
outstream< }

}
instream.close();
outstream.close();


}

你可能感兴趣的:(C++:替换文本文件中的某些字符)