2014-06-18 132 views
0

我有這樣的代碼,根據這個[pdf]加密數據。 1 我遇到的問題是代碼產生正確的輸出。當我的「貓」的OUTPUTFILE我得到正確的答案,但是如果我在文本編輯器打開該文件,我得到的結果是這樣的:C++寫入文本文件損壞

 
0068 6e74 7264 727a 0073 7558 6569 7965 
0061 6779 686f 6570 0064 6d62 6465 6358 
0074 7265 6568 6168 0075 7058 5862 7469 
0065 6e72 6d65 676c 0073 6377 6864 6e6f 
0073 6d6e 7479 7465 006c 6775 5869 6561 

預期輸出是:

 
hntrdrzsuXeiyeagyhoepdmbdecXtreehahupXXbtienrmeglscwhdnosmntytelguXiea 

使用這個字符串作爲原始的參數和鍵:玉米片和黑馬

 
sendresupplytothebridgebythechurchXXammoneededurgentlywithmagazinesXXX 

這是一個記憶的問題,是我流失敗?我覺得我正在俯視一些我什麼都看不到的東西。

這是怎麼加密:

string encrypt(string &key, string &toEncrypt) 
{ 
    int height= 1; 
    string result = ""; 

    vector<vector<char> > matrix(key.length(), vector<char>(2)); 

    string::iterator it=toEncrypt.begin(); 

    for (int i = 0; i < key.length(); i++) 
    { 
     matrix[i][0] = key.at(i); 
    } 


    // put info into matrix 
    printf("%s\n", key.c_str()); 
    while(it!=toEncrypt.end()) // while string still has more chars 
    { 
     //printf("newline----\n"); 
     for (int col = 0; col < key.length(); col++,it++) 
     { 
      if (it != toEncrypt.end()) 
      { 
       if(*it == '\0'){it++;} 
       matrix[col].push_back(*it); 
       printf("%c", *it); 
       continue; 
      } 
       if(col < key.length()) 
        matrix[col].push_back('X'); 
       printf("%c", 'X'); 

     } 
     height++; 
     printf("\n"); 
    } 
    //parse trough the matrix 
    printf("\n"); 
    BubbleSort(matrix); 
    printf("\n"); 

    printf("PAST BUBBLE SORT\n"); 
    for (int c = 0; c < key.length(); c++) 
    { 
     for (int r= 1; r < matrix[0].size(); r++) 
     { 
      result += matrix[c][r]; 
      printf("%c",matrix[c][r]); 
     } 
     printf("\n"); 
    } 


    printf("THE RESULT IS%s\n", result.c_str()); 
    return result; 
} 

這是我如何寫入文件:

string file = "\0"; 
printf("Please Enter the name of the file that contains the text to be encrypted with the extention.\n"); 
getline(cin, file, '\n'); 

string line; 
string encrypted; 

transposition_encr encryptor = transposition_encr(k1,k2); 

ifstream myfile (file.c_str()); 
if (myfile.is_open()) 
{ 
    while (getline (myfile,line)) 
    { 
     encrypted += line; 
    } 

    myfile.close(); 

} 
else 
{ 
    cout << "Unable to open file\n"; 
    return -1; 
} 

cout << encrypted << endl; 
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

cout<< encrypted << endl; 
encrypted = encryptor.encryption(line); 
cout<< encrypted << endl; 

string str = "outputfile-encrypted-str.txt"; 
ofstream myfile2(str.c_str()); 
    if (myfile2.is_open()) 
    { 
    myfile2 << encrypted; 
    myfile2.close(); 
    } 
    // else cout << "Unable to open file\n"; 

這裏是一個link的代碼

+1

您如何看待輸出? –

+0

輸出應該是這個使用鍵:CORNFLAKES和BLACKHORSE hntrdrzsuXeiyeagyhoepdmbdecXtreehahupXXbtienrmeglscwhdnosmntytelguXiea它與pdf中的相同。 – jcjunction

+0

我應該提到加密方法被調用兩次並將結果從一個字符串傳遞到下一個調用中 – jcjunction

回答

2

你開始

vector<char>(2) 

作爲matrix中的默認元素,然後是push_back()。最後,你丟棄第一個與

for (int r= 1; r < matrix[0].size(); r++) 

但仍然留給你一個空字節。您可能希望從一個空向量開始,並使用其所有元素。

+0

所以我需要一個2維向量,第一行是關鍵字,然後我開始推回到另一個向量中的下一行。 for循環僅用於獲取矢量的元素並將它們放入字符串中作爲檢查以確保內容是正確的。有趣的是,當我更改爲一個空向量時,我得到了一個段錯誤:11。 – jcjunction

+0

@jcjunction:這個答案是正確的。空字節傳播到輸出文件中,它們混淆了文本編輯器。我無法解析你的評論,但你對這些媒介不在意。 – Beta

+0

我想我有我的代碼混合然後。那麼這意味着什麼:向量>矩陣(key.length(),向量(2)); – jcjunction