2016-04-08 34 views
0

私はプログラムに取り組んでいましたが、今は解決策が見つけられません。私は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(); 
} 
+0

この複製はhttp://stackoverflow.com/questions/5343190/how-do-i-replace-all-instances-of-a-string-with-another-string? – fghj

+0

私はすでにlength関数を使っているので、これは重複していないので、これを解決することはできません。問題は、複数の記号を置き換えることであり、コードの最後にある置換関数が正しくないことです。出力は次のようになります:* 245 $aAnsätze "korporativer Marktwirtschaft"は韓国の朝鮮半島の中で、私の外出は次のようになります:* 245 $ a(テキストは2行目に続きますが、同じ行になければなりません) – silisun

答えて

1

ありがとうございました最初が、ここでは主にトップレベル・デザインに焦点を当てる):最後に

/* 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 FUNCTを使用することをお勧めしている場合大容量のファイルやバッチ処理を扱う際の効率を向上させます。 はstrstrは(forループの代わりに別の行く1内のすべてのmatchsを置き換えることができます)効率的かつ徹底的な両方であるfast pattern match in stringsで有名KMP algorithm、で実装されている間、文字列::検索はナイーブ逐次探索startegyを採用しています。

+0

ありがとう、非常に有用なアイデア!だから私は今クラスを実装しようとしていますが、まだいくつかの助けが必要です。 2つのクラス(fileとfilePcr)にはどのようなものが含まれていますか?答えをいただければ幸いです! – silisun

+0

@silisun Fileクラスは、基本的に 'vector 'です。ここでは、ベクトルの各要素はファイル内の行に対応し、 'openAndReadIn'と' saveAs'関数を提供し、両方とも 'bool'を返します成功した。 FileProcessorクラスには、ファイルの内容への保護されたアクセスを提供するための 'vector :: iterator'型の' cursor'がありますが、 'linkTo'関数はファイルへのアクセスを試みますが、ターゲットの場合はfalseを返しますファイルは分類されます。残りの関数は 'std :: string'の組み込み関数を使って簡単に実装できます。 – Lotayou

関連する問題