views:

39

answers:

2

I looked at boost's 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.

+1  A: 

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.

vanza
A: 

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;
Soonts