私はプログラムに取り組んでいましたが、今は解決策が見つけられません。私はfextファイルのいくつかの記号を置き換える必要があります。プログラムは "TIT"をコード番号 "* 245 $ a"に置き換えるだけです。同じ方法で他の文字を置き換えたい場合、プログラムは変わりません。テキストファイルにいくつかの置換えを実装する方法を知っている人はいますか? 5つ以上の看板を別の看板に置き換える方が良いかどうかを教えてください。C++は文字列(テキストファイル)内の単語を置換します
#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;
}
}
ファイルを変換するには、単に次のように行います(いくつかの関数やクラスを宣言して実装する必要があります は、あなたが複数の変換ルールを保存するためにC++ STLでmapを使用することができますあなたの
#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();
}
この複製はhttp://stackoverflow.com/questions/5343190/how-do-i-replace-all-instances-of-a-string-with-another-string? – fghj
私はすでにlength関数を使っているので、これは重複していないので、これを解決することはできません。問題は、複数の記号を置き換えることであり、コードの最後にある置換関数が正しくないことです。出力は次のようになります:* 245 $aAnsätze "korporativer Marktwirtschaft"は韓国の朝鮮半島の中で、私の外出は次のようになります:* 245 $ a(テキストは2行目に続きますが、同じ行になければなりません) – silisun