C++ 如何在C+中使用filestream将无符号int8数组正确读写到二进制文件+;

C++ 如何在C+中使用filestream将无符号int8数组正确读写到二进制文件+;,c++,filestream,C++,Filestream,我正在使用生成器生成无符号整数的随机序列,然后使用ofstream.write()将它们写入文件,方法如下: void CDataGenerator::GenerateRandom(std::string outputFileName, int length, bool UseEntireRange, int max) { std::ofstream file; file.open(outputFileName, std::ifstream::out | std::ifstrea

我正在使用生成器生成无符号整数的随机序列,然后使用ofstream.write()将它们写入文件,方法如下:

void CDataGenerator::GenerateRandom(std::string outputFileName, int length, bool UseEntireRange, int max) {
    std::ofstream file;
    file.open(outputFileName, std::ifstream::out | std::ifstream::binary);
    int count = 0;
    unsigned __int8* buf = new unsigned __int8[length];
    while (count < length-1) {
        int number = 0;
        if (UseEntireRange)
            number = rand();
        else {
            int rnd = rand();
            number = (int)((double)rnd / RAND_MAX * max);
        }
        int capacity = 0;
        if (number == 0)
            capacity = 1;
        else
            capacity = (int)(floor(log10(number)) + 1);
        for (int i = 0; i < capacity; ++i) {
            if (count >= length - 2)
                break;
            buf[count] = ((unsigned __int8)(number / (int)pow(10, capacity - i - 1)));
            number %= (int)pow(10, capacity - i - 1);
            ++count;            
        }       
        ++count;
        buf[count] = BCD_SEPARATOR;
    }
    file.write((__int8*)&buf[0], length);
    delete[] buf;
    file.close();   
}
Im我的测试生成器创建这样的序列 0x0 0xFF 0x5 0x6 0xFF 0x1 0x9 0xFF 0x8 0xFF 但从阅读流我得到了 0x0 0xCD 0x5 0x6 0xCD 0x1 0x9 0xCD 0x8 0xCD 序列
很明显,所有0xff都被0xcd取代。它是否与(uu int8*)强制转换相连接?如何解决它?

了解Visual Studio使用的CRT调试堆(我只是假设您使用的是Visual Studio),很可能0xCD值来自未初始化的堆内存。然后问题就变成了:为什么您在输出中看到了这一点?要找出原因,只需使用调试器单步执行GeneratorDOM函数/读取代码即可

从这一点可以看出问题出在哪里:

for (int i = 0; i < capacity; ++i) {
    if (count >= length - 2)
        break;
    buf[count] = ((unsigned __int8)(number / (int)pow(10, capacity - i - 1)));
    number %= (int)pow(10, capacity - i - 1);
    ++count; //Increment count ONCE
}

++count; //Increment count a SECOND time
buf[count] = BCD_SEPARATOR;

了解了VisualStudio使用的CRT调试堆(我只是假设您使用的是VisualStudio),很可能0xCD值来自未初始化的堆内存。然后问题就变成了:为什么您在输出中看到了这一点?要找出原因,只需使用调试器单步执行GeneratorDOM函数/读取代码即可

从这一点可以看出问题出在哪里:

for (int i = 0; i < capacity; ++i) {
    if (count >= length - 2)
        break;
    buf[count] = ((unsigned __int8)(number / (int)pow(10, capacity - i - 1)));
    number %= (int)pow(10, capacity - i - 1);
    ++count; //Increment count ONCE
}

++count; //Increment count a SECOND time
buf[count] = BCD_SEPARATOR;

在十六进制编辑器中查看文件时,是否使用正确的值写入?是否检查读取或写入是否成功?打开文件成功了吗?我还建议您使用标准的固定宽度整数类型,如
int8\t
from,而不是特定于编译器且不可移植的
\uu int8
。我还建议不要在C++中使用C样式的转换。相反,使用类似于重新解释(结果)的方法。不过,这些都不应该与您的问题有关。@RichardCriten,在hex编辑器中,它的值是错误的。您可能应该使用调试器,逐步完成对文件的代码写入。当您在hex编辑器中查看该文件时,是否使用了正确的值编写该文件?是否检查读取或写入是否成功?打开文件成功了吗?我还建议您使用标准的固定宽度整数类型,如
int8\t
from,而不是特定于编译器且不可移植的
\uu int8
。我还建议不要在C++中使用C样式的转换。相反,使用类似于重新解释(结果)的方法。不过,这些都不应该与您的问题有关。@RichardCriten,在hex编辑器中,它的值是错误的。您可能应该使用调试器并逐步完成代码写入文件的过程。
buf[count] = BCD_SEPARATOR;
++count;