C++ 写入损坏的文本文件

标签 c++ memory-management vector

我有根据此 [pdf] 加密数据的代码。 1 我遇到的问题是代码产生了正确的输出。当我“cat”输出文件时,我得到了正确的答案,但是如果我在文本编辑器中打开文件,我得到的结果如下所示:

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

The expected output is:

 hntrdrzsuXeiyeagyhoepdmbdecXtreehahupXXbtienrmeglscwhdnosmntytelguXiea

using this string as the original argument and the Keys: CORNFLAKES and BLACKHORSE

sendresupplytothebridgebythechurchXXammoneededurgentlywithmagazinesXXX

is this a memory issue, is my stream failing? I feel like I'm overlooking something I just can't see what.

this is how it encrypts:

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代码

最佳答案

你开始于

vector<char>(2)

作为 matrix 中的默认元素,然后 push_back() 到那些元素。最后,你用

丢弃第一个
for (int r= 1; r < matrix[0].size(); r++)

但这仍然会给您留下一个空字节。您可能想从一个空 vector 开始并使用它的所有元素。

关于C++ 写入损坏的文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24277321/

相关文章:

c++ - 为同一个指针变量重复调用复制构造函数内存泄漏?

c++ - 没有特殊标志的 cudaMallocHost() 和 cudaHostAlloc() 之间有什么区别吗?

c++ - CreatePen 不会造成内存泄漏吗?

在任意位置插入/放置元素时,C++ vector 会出现段错误

c++ - 如何生成 vector 组合

c++ - 无法以正确的方式执行其他程序

c++ - 递归 typedef 函数定义:std::function 返回自己的类型

android - 如何测量 Dalvik JVM 上的类内存大小?

c++ - 将元素从 char vector 复制到字符串 vector

c++ - 如何从 vector 中删除随机元素而不重复它们并保留元素顺序? C++