重写二进制文件的一部分

时间:2013-11-29 11:38:30

标签: c++ file fwrite fseek

我有带标题和元信息的图像文件,标题后存储的图像数据,我想重写图像数据,但图像部分周围的其他数据已损坏(标题)和文件被截断。

我怎样才能正确完成?

以下是代码:

FILE* f = _tfopen(fileName, _T("wb"));
if( f != NULL )
{
        uint64 headerSize = 8;
        char arr[2*3*3]; //w=3 h=2  RGB
        memset(arr,100,2*3*3);
        _fseeki64(f, headerSize, SEEK_SET);
        fwrite(arr,sizeof(char),sizeof(arr),f);
        fclose(f);
}

1 个答案:

答案 0 :(得分:4)

我认为您需要将文件中的开放模式从"wb"更改为"r+b"

来自cplusplus.com

"w" write: Create an empty file for output operations. If a file with the same name already exists, its contents are discarded and the file is treated as a new empty file.