2016-04-19 8 views
1

私はオンラインで見つけた単純な暗号化を使用しています。基本的に、私はファイルにストリーミングして、そのファイルが開いているかどうか(エラーメッセージを表示していないかどうか)チェックし、情報の暗号化中に配列の各要素に各行を入れます。その後、暗号化された情報を出力ファイルにストリームします。簡易暗号化が機能しない

しかし、output.txtファイルには何も表示されません。あなた自身でテストすれば、暗号化はうまく動作します。ここで

は私のコードです:

#include <string> 
#include <fstream> 
#include <sstream> // for ostringstream 
#include <iostream> 
#include <stdio.h> 
#include <algorithm> 

/* Credits to kylewbanks.com */ 
string encrypt (string content) { 
    char key[3] = {'K'}; //Any chars will work 
    string output = content; 

    for (int i = 0; i < content.size(); i++) 
     output[i] = content[i]^key[i % (sizeof(key)/sizeof(char))]; 

    return output; 
} 

int main() { 
    string input, line; 
    string content[10000]; 
    string encryptedContent[10000]; 

     int counter = 0, innerChoice = 0, i, finalCounter; 

     cout << "\tPlease enter the file name to encrypt!\n"; 
     cout << "\tType '0' to get back to the menu!\n"; 
     cout << "Input >> "; 

     cin >> input; 

     /* Reads in the inputted file */ 
     ifstream file(input.c_str()); 
     //fopen, fscanf 

     if(file.is_open()) { 

      /* Counts number of lines in file */ 
      while (getline(file, line)) { 
       counter++; 
      } 

      cout << counter; 

      finalCounter = counter; 

      for (i = 0; i < finalCounter; i++) { 
       file >> content[i]; 
       encryptedContent[i] = encrypt(content[i]); 
       cout << encryptedContent[i]; 
      } 
     } else { 
      cout << "\tUnable to open the file: " << input << "!\n"; 
     } 

     /* Write encryption to file */ 
     ofstream outputFile("output.txt"); 
     for (i = 0; i < finalCounter ; i++) { 
      outputFile << encryptedContent; 
     } 
     outputFile.close(); 
} 

何が間違っている任意の手掛かり?

+0

ていますが、フラッシング試してみましたか? –

+0

@JonnyHenlyプログラミングの初心者です。フラッシュとは何ですか? –

+1

ファイル内のすべての行を読み終えたら、ファイルポインタが最後にあります。最初に戻すには、 'file.seekg(0);'を実行する必要があります。 –

答えて

1
string content[10000]; 
string encryptedContent[10000]; 

これは間違っています。これは、20000文字列を作成しているためです(おそらく、データを読み取るために十分大きな文字配列を作成していると思われます)。

string content;で十分です。任意の長さの文字列を処理するようにサイズ変更することができます。

あなただけのバイナリでファイルを読み出し/書き込みする必要があります:ただの推測

int main() 
{ 
    string input = "input.txt"; 
    ifstream file(input, ios::binary); 
    if (!file.is_open()) 
    { 
     cout << "\tUnable to open the file: " << input << "!\n"; 
     return 0; 
    } 

    string plaintext; 

    //read the file  
    file.seekg(0, std::ios::end); 
    size_t size = (size_t)file.tellg(); 
    file.seekg(0); 
    plaintext.resize(size, 0); 
    file.read(&plaintext[0], size); 

    cout << "reading:\n" << plaintext << "\n"; 

    //encrypt the content 
    string encrypted = encrypt(plaintext); 

    //encrypt again so it goes back to original (for testing) 
    string decrypted = encrypt(encrypted); 

    cout << "testing:\n" << decrypted << "\n"; 

    /* Write encryption to file */ 
    ofstream outputFile("output.txt", ios::binary); 
    outputFile.write(encrypted.data(), encrypted.size()); 

    return 0; 
} 
+0

ああ、これはかなり意味がある!ありがとう、Barmak! :) –

関連する問題