modifying small part of an existing file
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;
精彩评论