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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010462037/article/details/70242011
#include <iostream>
#include <string>
#include <fstream>


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"<<endl;
outstream.open(out_path);
while(getline(instream,str)){
pos = str.find("hyu");//查找字符在string中第一次出现的位置
if (pos==string::npos)//判断是否存在“hyu”这个字符
{
cout<<"Can not find this string !"<<endl;
}else{
str.replace(pos,3,"www");//用www替换hyu
outstream<<str<<endl;
}

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


}
展开阅读全文

没有更多推荐了,返回首页