简体   繁体   English

在 C++ 代码部分,可能会写入两个字节

[英]in C++ code section, two bytes might be written

I am having a problem with a function, in visual studio 2019, written in c++:我在用 C++ 编写的 Visual Studio 2019 中遇到了一个函数问题:

#include <fstream>

//whatever...

void xorcrypt(string filename) { //the function
    ifstream inFile(filename, ios::ate);
    ifstream::pos_type size = inFile.tellg();
    char* memblock;
    memblock = new char[(unsigned int)size];
    inFile.seekg(0, ios::beg);
    inFile.read(memblock, size);
    inFile.close();
    for (long long i = 0; i < size;)
        for (int x = 0; x <= 255 && i < size; ++i, ++x)
            memblock[i] ^= x; //xor operation
    ofstream outFile;
    outFile.open(filename, ios::trunc);
    for (long long i = 0; i < size; ++i)
        outFile << memblock[i]; //The Problem
    outFile.close();
    delete[] memblock;
}

This code is Problematic, because Visual studio says that my dynamically initialized buffer, which holds the entire content of the file, might write 2 bytes to the file instead of 1... I do not know why this could e happening, and the function "XORcrypts" my files right most of the time, so I was hoping that someone Else Might Know why this is happening.这段代码是有问题的,因为 Visual Studio 说我的动态初始化缓冲区,它保存了文件的全部内容,可能会向文件写入 2 个字节而不是 1 个......我不知道为什么会发生这种情况,函数大多数时候“异或加密”我的文件是正确的,所以我希望其他人可能知道为什么会发生这种情况。

The code that is shown above takes a file, opens it to be read, and dumps the contents into a dynamically initialized char array, then the file is closed, truncated (wiped), and then the file is written to using a block of code which increments the XOR operation, per byte, to be used on the character.上面显示的代码获取一个文件,打开它以供读取,并将内容转储到动态初始化的 char 数组中,然后关闭文件,截断(擦除)文件,然后使用代码块写入文件这会增加要在字符上使用的每个字节的 XOR 操作。 once this is done with the writing operation, it deletes the character array and closes the file.写入操作完成后,它会删除字符数组并关闭文件。

I would, if possible, like to have a solution that does not create more dependency than the base c++, and fstream.如果可能的话,我希望有一个解决方案不会比基本的 c++ 和 fstream 产生更多的依赖。 Thanks in advance.提前致谢。

The warning occurs because the compiler can't tell what the value of size might be!出现警告是因为编译器无法判断size的值可能是什么! It could be less than 1 (or 2), if the file is empty.如果文件为空,它可能小于 1(或 2)。 Change your allocation to the following, to avoid the warning:将您的分配更改为以下内容,以避免出现警告:

memblock = new char[std::max((unsigned int)size, 2u)];

(Of course, you'll need to #include <algorithm> to get std::max() .) (当然,您需要#include <algorithm>来获得std::max() 。)

EDIT: For clarity, this is the warning I get without the 'fix':编辑:为清楚起见,这是我在没有“修复”的情况下收到的警告:

warning C6385: Reading invalid data from 'memblock':  the readable size is '(unsigned int)size.public: __cdecl std::fpos<struct _Mbstatet>::operator __int64(void)const ()*1' bytes, but '2' bytes may be read.

Is this the same as you see, Eric?这和你看到的一样吗,埃里克?

Apparently, I had defined the variable like this:显然,我已经定义了这样的变量:

   memblock = new char[(unsigned int)size];

However, there needed to be another character to hold the /0 ansi escape code, so here is the answer:但是,需要有另一个字符来保存 /0 ansi 转义码,所以这里是答案:

do this:做这个:

  memblock = new char[(unsigned int)size + 1];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM