C++实现的文本字符串替换功能
2010-12-12 10:10
337 查看
这是我最近刚写的一个文本文件中字符串替换的源代码。
实现的功能是输入要读写的文件名路径,然后输入要查找的字符串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;
}
else
sourcefile.replace(index,seeklength,replaceword);//用替换字符串替换指定位置的单词
}
inout.clear();//打开已存在的流对象,必须在每次偏移循环时清空
inout.seekp(0,ios::beg);//定位到文件开始位置
inout<<sourcefile;//重新写入替换后的文件内容
inout.close();
return 0;
}
实现的功能是输入要读写的文件名路径,然后输入要查找的字符串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;
}
else
sourcefile.replace(index,seeklength,replaceword);//用替换字符串替换指定位置的单词
}
inout.clear();//打开已存在的流对象,必须在每次偏移循环时清空
inout.seekp(0,ios::beg);//定位到文件开始位置
inout<<sourcefile;//重新写入替换后的文件内容
inout.close();
return 0;
}
相关文章推荐
- Java实现--替换文本文件中所有符合条件的字符串
- C++实现字符串替换的两种方法
- 【C/C++开发】C++实现字符串替换的两种方法
- python-实现xml字符串替换功能
- PHP字符串替换函数strtr()的功能实现讲解
- 替换字符串中的空格(C++/Java实现)
- java实现查找文本内容替换功能示例
- [C++字符串替换函数的实现]
- Java基于正则表达式实现的替换匹配文本功能【经典实例】
- C++实现字符串替换的两种方法
- 在C++中实现类似C#的字符串格式功能
- ASE12.x与15.x中的字符串替换功能的实现
- (原创)实现textarea内字符串选择查询替换功能
- C++实现将一个字符串中的字符替换成另一个字符串的方法
- 【编程珠玑】第十五章--字符串:统计文本中单词数量(C++的Map&C的结构体实现&POJ2418)
- PHP字符串替换函数strtr()的功能实现讲解
- java过滤敏感词实现字符串替换功能
- 请用c++ 实现stl中的string类,实现构造,拷贝构造,析构,赋值,比较,字符串相加,获取长度及子串等功能
- 实现textarea内字符串选择查询替换功能资料