| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1098 人关注过本帖
标题:文本内容替换
只看楼主 加入收藏
最近不在
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:5
帖 子:204
专家分:842
注 册:2010-2-28
结帖率:95%
收藏
已结贴  问题点数:20 回复次数:3 
文本内容替换
程序代码:
// Note:Your choice is C++ IDE
#include <sstream>
#include <fstream>
#include <string>
#include <iostream>
#include <vector>
using namespace std;

int nCount = 0;

string& replaceAll(string& context, const string& from, const string& to)
{
    size_t lookHere = 0;
    size_t foundHere;
    while((foundHere = context.find(from, lookHere)) != string::npos)
    {
        nCount++;
        context.replace(foundHere, from.size(), to);
    }
    return context;
}

int main()
{
    ifstream ifile("g:\\123.txt");
    string str;
    vector<string> text;
    string from;
    string to;
    int nSum = 0;
   
    cout<<"文本内容"<<str<<endl;
    while(getline(ifile, str))
    {
        cout<<str<<endl;
    }
    ifile.close();
    ifile.clear();
   
     cout<<"请输入要替换的内容:";
     cin>>from;
     cout<<"请输入要替换为的内容:";
     cin>>to;
   
    ifile.open("g:\\123.txt");
    while(getline(ifile, str))
    {
        replaceAll(str, from, to);
        nSum += nCount;
        text.push_back(str);
    }
   
    ifile.close();
    ifile.clear();
   
    cout<<"共修改"<<nSum<<""<<endl;
   
    if(text.size())
    {
        cout<<"替换后:"<<endl;
        ofstream ofile("g:\\123.txt");
        for(int i = 0; i != text.size(); ++i)
        {
            ofile<<text[i]<<endl;
            cout<<text[i]<<endl;
        }
        ofile.close();
        ofile.clear();
    }
    else
    {
        cout<<"没有找到可以替换的!";
        while(getline(ifile, str))
        {
            cout<<str<<endl;
        }
    }
  
    return 0;
}
搜索更多相关主题的帖子: 文本 
2010-09-19 16:23
ciweitou163
Rank: 7Rank: 7Rank: 7
来 自:河北 石家庄
等 级:黑侠
威 望:1
帖 子:144
专家分:528
注 册:2008-10-4
收藏
得分:6 


  • 满眼生机转化钧;天工人巧日争新。
2010-09-19 20:13
x_wangyue
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:34
专家分:144
注 册:2010-8-30
收藏
得分:6 
顶一哈
2010-09-19 20:34
ljt
Rank: 6Rank: 6
等 级:侠之大者
威 望:3
帖 子:191
专家分:431
注 册:2009-4-6
收藏
得分:6 
接分
2010-09-20 11:03
快速回复:文本内容替换
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018610 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved