|
发表于 2013-5-9 13:16:32
|
显示全部楼层
add_p_p 函数有点问题。
改过的代码vs2010:
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <cstdlib>
- using namespace std;
- void add_p_p(ifstream& ins,ofstream& outs);
- string& replace_all(string& str,const string& old_value,const string& new_value)
- {
- while(true) {
- string::size_type pos(0);
- if( (pos=str.find(old_value))!=string::npos )
- str.replace(pos,old_value.length(),new_value);
- else break;
- }
- return str;
- }
- string& replace_all_distinct(string& str,const string& old_value,const string& new_value)
- {
- for(string::size_type pos(0); pos!=string::npos; pos+=new_value.length()) {
- if( (pos=str.find(old_value,pos))!=string::npos )
- str.replace(pos,old_value.length(),new_value);
- else break;
- }
- return str;
- }
- int main()
- {
- // string str = "lihan";
- // str.replace(str.begin(),str.end()-1,2,'s');
- // cout<<str;
- // cout << replace_all(string("12212"),"2","3") << endl;
- // cout << replace_all_distinct(string("12212"),"12","21") << endl;
- // return 0;
- ifstream fin;
- ofstream fout;
- fin.open("E:\\1.txt");
- fout.open("E:\\2.txt");
- if(fin.fail())
- {
- cout<<"open error"<<endl;
- exit(1);
- }
- if(fout.fail())
- {
- cout<<"out error"<<endl;
- exit(1);
- }
- add_p_p(fin,fout);
- fin.close();
- fout.close();
- return 0;
- }
- void add_p_p(ifstream& ins,ofstream& outs)
- {
- char name[4000];
- while(!ins.eof())
- {
- ins.getline(name,4000,'?');
- string s(name);
- replace_all_distinct(s,"command","order");
- outs<<s.c_str();
- // if(strcmp(name,"command")==0)
- // outs<<"order";
- // else
- // outs<<name;
- }
- }
复制代码 参考资料:
http://blog.csdn.net/lihan6415151528/article/details/4732704
|
|