#include<cstdlib>
#include<string>
#include<cstring>
#include<fstream>
using namespace std;
void myreplace(const string& filename,const string& tofind,const string& toreplace)
{
ifstream fin(filename.c_str(),ios_base::binary);
string str(1024*1024*2,0);
fin.read(&str[0],2*1024*1024);
fin.close();
ofstream fout(filename.c_str(),ios_base::binary);
string::size_type beg=0,pos,find_size=tofind.size(),replace_size=toreplace.size();
while((pos=str.find(tofind,beg))!=string::npos)
{
fout.write(&str[beg],pos-beg);
fout.write(&toreplace[0],replace_size);
beg=pos+find_size;
}
fout.write(&str[beg],strlen(str.c_str())-beg);
fout.close();
}
int main()
{
myreplace("abca.txt","123ABCD123","456EFG456");
}
C语言实现文本替换
最新推荐文章于 2024-06-27 12:02:04 发布