通过内存映射 复制文档内容 linux环境编程
来源:互联网 发布:java修改文件夹权限 编辑:程序博客网 时间:2024/05/16 16:58
#include#include#include "apue.h"#include#include#include#include#include#include#define BUFFSIZE 4096intmain(int argc , char *argv[]){ int fdin,fdout; struct stat statbuf; void *src,*dst; if(argc!=3) err_quit("insert 3 1arg \n"); if((fdin=open(argv[1],O_RDONLY))<0) err_sys("can't open %s",argv[1]); if((fdout=open(argv[2],O_RDWR|O_CREAT|O_TRUNC,FILE_MODE))<0) err_sys("can't open %s",argv[2]); if(fstat(fdin,&statbuf)<0) err_sys("fstat err"); printf("statbuf.st_size =%ld\n",statbuf.st_size); if(lseek(fdout,statbuf.st_size-1,SEEK_SET)==-1) err_sys("lseek err"); if(write(fdout,"a",1)<0) err_sys("write fdout err"); if((src=mmap(0,statbuf.st_size,PROT_READ,MAP_SHARED,fdin,0))==MAP_FAILED) err_sys("mmap src err"); if((dst=mmap(0,statbuf.st_size,PROT_READ|PROT_WRITE,MAP_SHARED,fdout,0))==MAP_FAILED) err_sys("mmap dst err"); memcpy(dst,src,statbuf.st_size); exit(0); }