开发者

modifying small part of an existing file

开发者 https://www.devze.com 2023-01-13 14:40 出处:网络
I looked at boost\'s开发者_如何学C mapped_file, and CreateFileMapping/MapViewOfFile, but they seem overly complicated to use.

I looked at boost's开发者_如何学C mapped_file, and CreateFileMapping/MapViewOfFile, but they seem overly complicated to use.

Anything simpler I can use to overwrite a few bytes here and there in an existing file? Performance is not a very high consideration.


You can use the standard C library directly. fopen then fseek to where you want to write stuff. Or, if you want to be fancy, you can also try mmap.


Something like this (untested, and you should also check the HRESULTS error codes):

CAtlFile f;
f.Create( L"MyFile.txt", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, OPEN_ALWAYS );

CAtlFileMapping<BYTE> map;
map.MapFile( f , 0, 0, PAGE_READWRITE, FILE_MAP_ALL_ACCESS );

printf( "%d bytes\n", (int)map.GetMappingSize() );

// Overwrite the 3-rd byte with 21
map[2] = 21;
0

精彩评论

暂无评论...
验证码 换一张
取 消