我對C++(從C#中移出)有點新手,所以我不確定這裏發生了什麼。我想要做的是從文件中讀取圖像並將其寫入輸出文件,但每當我做文件的部分顯示爲損壞。fwrite()文件損壞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;
先進的感謝!
啊!我用二進制模式打開了最初的文件,但不是輸出文件。你真棒,謝謝你! – Lander