#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();
}
#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();
}