代码:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#define FILELENGTH 80
int main(void)
{
int fd=-1;
char buf[]="quick brown fox jumps over the lazy dog";
char *ptr=NULL;
fd=open("mmap.txt",O_RDWR|O_CREAT|O_TRUNC,S_IRWXU);
if(-1==fd)
{
return -1;
}
lseek(fd,FILELENGTH-1,SEEK_SET);
write(fd,buf,1);
ptr=(char*)mmap(NULL,FILELENGTH,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
if((char*)-1==ptr)
{
printf("mnap failure\n");
close(fd);
return -1;
}
memcpy(ptr+16,buf,strlen(buf));
munmap(ptr,FILELENGTH);
close(fd);
return 0;
}
编译运行