Files
2025-04-29 23:31:09 +08:00

37 lines
762 B
C++

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
int main(int argc, char *argv[])
{
int fd = open("c.txt", O_RDWR);
if (fd == -1) {
perror("[open file] ");
_exit(1);
}
int len = lseek(fd, 0, SEEK_END);
void* ptr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); //创建内存映射区
if (ptr == MAP_FAILED) {
perror("[mmap fail] ");
exit(1);
}
((char *)ptr)[3] = 'A';
((char *)ptr)[4] = 'B';
((char *)ptr)[5] = 'C';
printf("%s\n", (char*)ptr); //释放内存映射区
munmap(ptr, len);
close(fd);
return 0;
}