C ++替换字符串中的单词(文本文件)

时间:2016-04-08 22:54:06

标签: c++ string fstream

所以我参与了我的计划,现在我处于无法找到解决方案的地步。我需要在fext文件中替换一些更多的标志,现在程序只用代码号“* 245 $ a”替换“TIT”,如果我想用同样的方式替换其他字母,程序不会改变。有谁知道如何在文本文件中实现更多替换?让我知道是否有更好的选择,用另外的标志替换5个以上的标志。 谢谢

#include <fstream>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;


int main()
{

    char dateiname[64], kommando[64];

    ifstream iStream;

    cout << "Choose an activity" << endl <<
    " s - search " << endl <<
    " c - convert" << endl <<
    " * - end program" << endl;
    cin.getline(kommando,64,'\n');
    switch(kommando[0])
    {
        case 'c':
            cout << "Enter a text file!" << endl;
            cin.getline(dateiname,64,'\n');
            iStream.open("C://users//silita//desktop//schwarz.txt");

        case 's':
            break;
        case '*':
            return 0;
        default:
            cout << "I can not read " << kommando << endl;
    }

    if (!iStream)
    {
        cout << "The File" << dateiname << "does not exist." << endl;
    }

    string s;
    char o[] = "TIT";
    while (getline(iStream, s))
    {
        while(s.find(o, 0) < s.length())
            s.replace(s.find(o, 0), s.length() - s.find(o, 3),"*245$a");

        cout << s << endl;
    }

    iStream.close();
}

0 个答案:

没有答案
相关问题