C++实现的文本字符串替换功能

来源:互联网 发布:热血屠龙光翼进阶数据 编辑:程序博客网 时间:2024/05/14 07:31

 这是我最近刚写的一个文本文件中字符串替换的源代码。

实现的功能是输入要读写的文件名路径,然后输入要查找的字符串A和替换字符串B,执行结果是将文件中的所有字符串A替换为字符串B。

注:此源代码有许多瑕疵和效率低下的地方,还望大家多多指教,勿见笑!

#include <iostream>#include <fstream>#include <string>using namespace std;int main(){char buffer[200];//临时缓存char filename[50]; string filein;//保存要替换的源文件名cout<<"Please input the source of file's name:"<<endl;cin>>filein;strcpy(filename,filein.c_str());//源文件名字符串转换为字符数组fstream inout(filename);//以读写方式打开某一文件if(!inout.is_open())//判断是否打开{cout<<"Error opening file";exit(1);}//以下注释是测试代码/*while(!inout.eof()){inout.getline(buffer,100);//读入打开文件的每一行cout<<buffer<<endl;}*/string sourcefile;int seeklength;string seekword,replaceword;//定义要查找的单词和替换为的单词字符串变量char seekchar[20],replacechar[20];cout<<"Please input the name of seek and replace:"<<endl;cin>>seekword>>replaceword;strcpy(seekchar,seekword.c_str());//将查找单词字符串转换为字符数数组strcpy(replacechar,replaceword.c_str());//将替换单词字符串转换为字符数数组seeklength = seekword.length();//记录要查找单词的长度while(!inout.eof()){inout.getline(buffer,200);//读入打开文件的每一行sourcefile.append(buffer);//将读取文件的文件内容存到字符串中sourcefile.append("/r/n");//追加换行符}int index=0;bool mark=true;while(mark){index = sourcefile.find(seekword,index);//定位要替换的单词,记录其开始位置if(index == string::npos)//替换完毕或没发现单词的处理语句{cout << "Didn't find the word or replace the word is OK!" << endl;mark=false;}elsesourcefile.replace(index,seeklength,replaceword);//用替换字符串替换指定位置的单词}inout.clear();//打开已存在的流对象,必须在每次偏移循环时清空inout.seekp(0,ios::beg);//定位到文件开始位置inout<<sourcefile;//重新写入替换后的文件内容inout.close();return 0;}