#include<stdio.h> #include<stdlib.h> #include<sys/mman.h> #include<sys/stat.h> #include<sys/types.h> #include<fcntl.h> #define FILE_LENGTH 0x1000 int main() { void* file_memory; int fd; fd = open("test", O_RDWR|O_CREAT); if(fd < 0) printf("open test.txt error!/n"); lseek(fd, FILE_LENGTH, SEEK_SET); write(fd, " ", 1); lseek(fd, 0, SEEK_SET); file_memory = mmap(NULL, FILE_LENGTH, PROT_WRITE, MAP_SHARED, fd, 0); if(file_memory == MAP_FAILED) printf("mmap failed/n"); close(fd); //printf("%s/n", file_memory); sprintf((char*)file_memory, "%d/n", 1234); munmap(file_memory, FILE_LENGTH); }