我对C++ (从C#迁移)还是个新手,所以我不太清楚这里到底发生了什么。我尝试做的是从文件中读取图像并将其写入输出文件,但无论何时,文件的某些部分似乎已损坏。
我检查了内存中的数据,它实际上是匹配的,所以我相信罪魁祸首一定是fwrite()出了什么问题,尽管它总是可能是我做错了什么。
下面是一些示例数据:http://pastebin.com/x0eZin6K
我的代码是:
// used to figure out if reading in one giant swoop has to do with corruption
int BlockSize = 0x200;
// Read the file data
unsigned char* data = new unsigned char[BlockSize];
// Create a new file
FILE* output = fopen(CStringA(outputFileName), "w+");
for (int i = 0; i < *fileSize; i += BlockSize)
{
if (*fileSize - i > BlockSize)
{
ZeroMemory(data, BlockSize);
fread(data, sizeof(unsigned char), BlockSize, file);
// Write out the data
fwrite(data, sizeof(unsigned char), BlockSize, output);
}
else
{
int tempSize = *fileSize - i;
ZeroMemory(data, tempSize);
fread(data, sizeof(unsigned char), tempSize, file);
// Write out the data
fwrite(data, sizeof(unsigned char), tempSize, output);
}
}
// Close the files, we're done with them
fclose(file);
fclose(output);
delete[] data;
delete fileSize;
https://stackoverflow.com/questions/6976992
复制相似问题