I have a file "b.txt", which is 16 bytes long:
b.txt:
00 12 32 a5 6f ff 11 67 00 00 00 11 fa 90 44 66
I use the following code to change the first 2 bytes of b.txt, and it works.
Code:
int fd, i;
char *mf;
char *buf ;
fd = open("b.txt", O_RDWR);
mf = mmap(0, 2, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
for(i = 0; i < strlen(buf); i++)
mf[i] = buf[i];
msync(mf, 2, MS_SYNC);
however, if i use
Code:
mf = mmap(0, 2, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 2);
to change the 3rd and 4th (i.e. 32 and a5) of b.txt, segmentation fault occurs.
Can anyone tell me how to map the middle part of a file into memory and do changes to it?