鱼C论坛

 找回密码
 立即注册
查看: 3801|回复: 5

哥们,帮帮新手,我想把文件里代码的单词改成另一个。

[复制链接]
发表于 2013-5-9 13:16:31 | 显示全部楼层 |阅读模式
8鱼币
#include <iostream>
#include <fstream>
#include <string.h>
#include <cstdlib>
using namespace std;
void add_p_p(ifstream& ins,ofstream& outs);
int main()
{
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];
ins.getline(name,4000,'?');
while(!ins.eof())
{
  if(strcmp(name,"command")==0)  
   outs<<"order";
  else  
   outs<<name;
  ins.getline(name,4000,'?');
}
}
但是输出文件中并没有改掉command.咋回事啊。

最佳答案

查看完整内容

add_p_p 函数有点问题。 改过的代码vs2010: 参考资料: http://blog.csdn.net/lihan6415151528/article/details/4732704
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-5-9 13:16:32 | 显示全部楼层
add_p_p 函数有点问题。
改过的代码vs2010:
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cstdlib>
  5. using namespace std;
  6. void add_p_p(ifstream& ins,ofstream& outs);

  7. string&   replace_all(string&   str,const   string&   old_value,const   string&   new_value)     
  8. {     
  9.         while(true)   {     
  10.                 string::size_type   pos(0);     
  11.                 if(   (pos=str.find(old_value))!=string::npos   )     
  12.                         str.replace(pos,old_value.length(),new_value);     
  13.                 else   break;     
  14.         }     
  15.         return   str;     
  16. }     

  17. string&   replace_all_distinct(string&   str,const   string&   old_value,const   string&   new_value)     
  18. {     
  19.         for(string::size_type   pos(0);   pos!=string::npos;   pos+=new_value.length())   {     
  20.                 if(   (pos=str.find(old_value,pos))!=string::npos   )     
  21.                         str.replace(pos,old_value.length(),new_value);     
  22.                 else   break;     
  23.         }     
  24.         return   str;     
  25. }     

  26. int main()
  27. {
  28. //         string str = "lihan";  
  29. //         str.replace(str.begin(),str.end()-1,2,'s');  
  30. //         cout<<str;  
  31. //         cout   <<   replace_all(string("12212"),"2","3")   <<   endl;     
  32. //         cout   <<   replace_all_distinct(string("12212"),"12","21")   <<   endl;   
  33. //         return 0;
  34.         ifstream fin;
  35.         ofstream fout;
  36.         fin.open("E:\\1.txt");
  37.         fout.open("E:\\2.txt");
  38.         if(fin.fail())
  39.         {
  40.                 cout<<"open error"<<endl;
  41.                 exit(1);
  42.         }
  43.         if(fout.fail())
  44.         {
  45.                 cout<<"out error"<<endl;      
  46.                 exit(1);
  47.         }
  48.         add_p_p(fin,fout);
  49.         fin.close();
  50.         fout.close();
  51.         return 0;
  52. }
  53. void add_p_p(ifstream& ins,ofstream& outs)
  54. {
  55.         char name[4000];
  56.         while(!ins.eof())
  57.         {
  58.                 ins.getline(name,4000,'?');
  59.                 string s(name);
  60.                 replace_all_distinct(s,"command","order");
  61.                 outs<<s.c_str();
  62. //                 if(strcmp(name,"command")==0)  
  63. //                         outs<<"order";
  64. //                 else  
  65. //                         outs<<name;
  66.         }
  67. }
复制代码
参考资料:
http://blog.csdn.net/lihan6415151528/article/details/4732704
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-5-9 13:28:03 | 显示全部楼层
无回帖,不论坛,这才是人道。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-5-9 18:18:33 | 显示全部楼层
咋没人呢!!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-5-9 20:48:36 | 显示全部楼层
强烈支持楼主ing……
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-5-10 12:33:04 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-28 20:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表