gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 05:44:21 30 4
gpt4 key购买 nike

所以我在我的程序上工作,现在我无法找到解决方案。我需要在 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();
}

最佳答案

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

#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 采用简单的顺序搜索策略,而 strstr 使用著名的 KMP algorithm 实现对于 fast pattern match in strings ,既高效又彻底(可以一次性替换所有匹配项,而不是另一个 for 循环)。

关于C++ 替换字符串中的单词(文本文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36510581/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com