2017-10-18 6 views
-1

私は現在、テキストファイル(plaintext.txtという名前)をキーファイルとともにアルファベットに置き換え、暗号文を作成するプログラムを開発中です私はそれらを一緒に混ぜるためのコマンドを実行します。下記に示すように、作業コードは次のとおりです。上記のコードのためのstd :: stringstreamの出力がstd :: stringと同じに動作しない

string text; 
string cipherAlphabet; 

string text = "hello"; 
string cipherAlphabet = "yhkqgvxfoluapwmtzecjdbsnri"; 

string cipherText; 
string plainText; 

bool encipherResult = Encipher(text, cipherAlphabet, cipherText); 
bool decipherResult = Decipher(cipherText, cipherAlphabet, plainText); 

cout << cipherText; 
cout << plainText; 

出力はしかし、私は取得文字列に私の「テキスト」と「cipherAlphabet」を変換したい

fgaam 
hello 

の下になりますそれらの両方は異なるテキストファイルを介して。

string text; 
string cipherAlphabet; 


std::ifstream u("plaintext.txt"); //getting content from plainfile.txt, string is text 
std::stringstream plaintext; 
plaintext << u.rdbuf(); 
text = plaintext.str(); //to get text 


std::ifstream t("keyfile.txt"); //getting content from keyfile.txt, string is cipherAlphabet 
std::stringstream buffer; 
buffer << t.rdbuf(); 
cipherAlphabet = buffer.str(); //get cipherAlphabet;*/ 

string cipherText; 
string plainText; 

bool encipherResult = Encipher(text, cipherAlphabet, cipherText); 
bool decipherResult = Decipher(cipherText, cipherAlphabet, plainText); 

cout << cipherText; 
cout << plainText; 

しかし、私がこれを行うと、出力もエラーもなくなるのですか?これで私を助けてくれる人がいますか?ありがとうございました!!あなたがtextを抽出するために、上記のコード行を使用する場合

+0

状態がまだ良好かどうかを確認する前に、必ず 'if(t)'をチェックしてください。 –

+0

あなたはファイルを読んでいません。ファイルをstd :: stringに読み込んで処理してください。あなたはどのようにGoogleをすることができます。 –

+0

@AnonMail OPは、実際には 'rdbuf()'を使ってファイルを読み込んでいます。 –

答えて

1
std::ifstream u("plaintext.txt"); //getting content from plainfile.txt, string is text 
std::stringstream plaintext; 
plaintext << u.rdbuf(); 
text = plaintext.str(); //to get text 

、あなたもファイル内の任意の空白文字を取得している - 最も可能性の高い改行文字を。次のコードブロックを簡略化してください。

std::ifstream u("plaintext.txt"); 
u >> text; 

暗号を読み取るには、同じ変更を行う必要があります。

改行文字を除外するには空白を含める必要がある場合は、std::getlineを使用します。

std::ifstream u("plaintext.txt"); 
std::getline(u, text); 

複数行のテキストを扱う必要がある場合は、プログラムを少し変更する必要があります。

+0

早速返信いただきありがとうございます!しかし、私は文字列の間のスペースを読み取る関数の能力を利用したいと思います。 今のところ、テキストファイルの1行の文字列で読み込もうとしています。以前はwhileやforループを使ってテキストファイルのスペースを読み込むのが難しかったし、ループを使わずにこのテキストファイルを読んでいた。 – fabian

+0

@fabianの場合、 'std :: getline'を使う必要があります。 'std :: getline(u、text);'スペースは含まれますが、終わりの改行文字は除外されます。 –

+0

ああ私の神は魅力のように動作します!手伝ってくれてどうもありがとう!!! :D – fabian

関連する問題