martin71
martin71

Reputation: 123

modifying small part of an existing file

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.

Upvotes: 0

Views: 156

Answers (2)

Soonts
Soonts

Reputation: 21966

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;

Upvotes: 0

vanza
vanza

Reputation: 9903

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.

Upvotes: 1

Related Questions