第五章

xiaoxiao2021-02-28  29

#include <sys/mman.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <string.h> int main(int argc, char **argv) { int fd; //file descriptor int length; char *mapped_mem; //存储页大小 int pagesize=0; int choose=0; //删除的内容多少 int where=0; //删除的位置 pagesize=getpagesize(); //获取页大小 fd = open(argv[1], O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); //打开一 个文件 length=lseek(fd, 0, SEEK_END); //到达文件结束,获取文件大小 mapped_mem = mmap(NULL, length/pagesize+2, PROT_READ|PROT_WRITE,MAP_SHARED,fd, 0); printf("\n请输入要删除内容(size<%d):",length);//要求删除的内容,内容量小 于1个页 scanf("%d",&choose); printf("\n请输入要删除的位置(size<%d):",length); scanf("%d",&where); memmove(mapped_mem+where,mapped_mem+where+choose,length-where);//移动覆>盖要删除的内容 msync(mapped_mem,length-where,MS_SYNC|MS_INVALIDATE); //要求回写磁盘 munmap(mapped_mem, length); return 0; }

转载请注明原文地址: https://www.6miu.com/read-2800016.html

最新回复(0)