簡體   English   中英

C ++寫入文本文件已損壞

[英]C++ writing to text file corrupted

我有這段代碼可以根據此[pdf]加密數據。 1我遇到的問題是代碼產生正確的輸出。 當我“捕獲”輸出文件時,我得到正確的答案,但是,如果我在文本編輯器中打開文件,則會得到如下結果:

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

使用此字符串作為原始參數和鍵:CORNFLAKES和BLACKHORSE

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";

這是代碼的鏈接

你開始

vector<char>(2)

作為matrix默認元素,然后將push_back()為這些元素。 最后,您將第一個與

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

但這仍然留下一個空字節。 您可能想從一個空向量開始並使用其所有元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM