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

C++ replace words in a string (text file)

本文关键字:单词 文件 文本 替换 字符串 C++      更新时间:2023-10-16

所以我在程序上工作,现在我找不到解决方案。我需要在fext文件中替换更多的符号,因为现在程序只将"TIT"替换为代码号"*245$a",如果我想以相同的方式替换其他字母,程序不会更改。有人知道我如何在文本文件中实现更多的替换吗?请告诉我是否有更好的选择,用其他标志替换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();
}

您可以在C++STL中使用map来存储多个转换规则:

#include<map>
#include<algorithm>
using namespace std;
map<string,string> convertRules;
typedef map<string,string>::iterator MIT;
void setConvertRules(int numOfRules){
    string word,code;
    for(int i = 0 ; i < numOfRules; ++i){
        cin>>word>>code;
        //Use code as search key in order to decrypt
        //If you want to encrypt, use convertrules[word] = code;
        convertRules[code] = word;
    }
}

要转换文件,只需执行以下操作(一些函数和类需要首先声明和实现,但这里我们主要关注顶层设计):

/* Detailed class implementations are omitted for simplicity */
//a class to store contents of a file
class File;
//a processor to read, insert and overwrite certain file
class FileProcessor;
void FileProcessor::convert(const string &code, const string &word){
    cursor == file.begin();
    while(cursor != fp.end()){
         _fp.convertNextLine(code,word);
    }
}
File file;
FileProcessor filePcr;
int main()
    const string sourceDir = "C://users//silita//desktop//schwarz.txt";
    const string destDir = "C://users//silita//desktop//schwarz_decrypted.txt";
    //Open a .txt file and read in its contents
    if (!file.openAndReadIn(sourceDir)){
        cerr << "The File" << sourceDir << "does not exist." << endl;
        abort();
    }
    //Try to link processor to open file
    if(!fp.linkTo(file)){
        cerr << "Access to file" << sourceDir << "is denied." << endl;
        abort();
    }
    //iterator is like a more safe version of C-style pointer
    //the object type is a string-string pair
    for(MIT it = convertRules.begin(); it != convertRules.end(); ++it){
        fp.convert(it->first, it->second);
    }
    file.saveAs(destDir);
    return 0;
}

最后,如果我建议您在处理大文件或批处理时使用C样式strstr函数以提高效率。string::find采用了一个朴素的顺序搜索startegy,而strstr是用著名的KMP算法实现的,用于字符串中的快速模式匹配,它既高效又彻底(可以一次替换所有匹配,而不是另一次替换循环)。