fwrite()文件损坏c++
fwrite() File Corruption C++
我是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;
您是否在Windows上运行此代码?对于不需要文本翻译的文件,必须以二进制模式打开:
FILE* output = fopen(CStringA(outputFileName), "wb+");
输出文件如下所示:
07 07 07 09 09 08 0A 0C 14 0D 0C
07 07 07 09 09 08 0D 0A 0C 14 0D 0C
^^
C运行库帮助您将n
转换为rn
。
您需要通过在模式中添加"b"来打开二进制文件。
http://www.cplusplus.com/reference/clibrary/cstdio/fopen/相关文章:
- 为什么会发生堆损坏
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 平均图像时图像损坏
- 如何针对特定情况调试和修复此双自由内存损坏问题
- 为什么C中的通用链表中存储的数据已损坏
- gdb错误:Backtrace已停止:上一帧与此帧相同(堆栈已损坏?)
- C++双重释放或损坏(out)
- 捕获标准输出以压缩并使用 CTRL-C 中断会给出损坏的 zip 文件
- 使用全局声明的向量时,C++双重释放错误/损坏
- 变量周围的堆栈'...'已损坏
- 运行时检查失败 #2 变量"A"周围的堆栈已损坏
- 检测到堆损坏:在正常块 c++ 动态 2D 数组之后
- 删除字符串后C++检测到堆损坏
- 两个垫子的 OpenCV 数据是相同的,但使用 Mat::at 检索时的值已损坏
- 我可以写入关闭的套接字并强制纠正损坏的管道错误吗?
- 损坏的结构字符数组 - sqlite C++
- 运行时检查失败 #2 - 变量"e"周围的堆栈已损坏。发生
- zlib 膨胀在使用小缓冲区时会损坏
- 正在调试 malloc():新内存损坏
- 变量周围的堆栈'sortArray'已损坏