通过内存映射 复制文档内容 linux环境编程

#include
#include
#include "apue.h"
#include
#include
#include
#include
#include
#include
#define BUFFSIZE 4096
int
main(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);
 
}

你可能感兴趣的:(编程,linux,struct,文档,insert,DST)