Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1018952
  • 博文数量: 162
  • 博客积分: 3887
  • 博客等级: 中校
  • 技术积分: 1617
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-06 19:05
文章分类

全部博文(162)

文章存档

2015年(4)

2014年(7)

2013年(10)

2012年(16)

2011年(89)

2010年(36)

分类: C/C++

2011-06-21 15:24:27

#include
#include
介绍:使得一个磁盘文件与存储空间中的一个缓存相映射,相当于从缓存中取数据,就等于读文件中的字节。相反,将数据存入缓存,就等于将字节写入文件。
caddr_t mmap(caddr_t addr, size_t len, int prot, int flag, int filedes, off_t off);

#include
#include
#include
#include
#include
#include

#ifndef MAP_FILE
#define MAP_FILE 0
#endif

int main(int argc, char *argv[])
{
  int fdin;
  int fdout;
  char *src;
  char *dst;
  struct stat statbuf;

  if(argc != 3)
    printf("usage: a.out ");
  if((fdin = open(argv[1], O_RDONLY)) < 0)
    printf("Can't open %s for reading", argv[1]);
  if((fdout = open(argv[2], O_RDWR | O_CREAT | O_TRUNC)) < 0)
    printf("Can't create %s for writing", argv[1]);

  if(fstat(fdin, &statbuf) < 0)
    printf("fstat error");
  if(lseek(fdout, statbuf.st_size - 1, SEEK_SET) == -1)
    printf("lseek error");
  if(write(fdout, "", 1) != 1)
    printf("write error");
  if((src = mmap(0, statbuf.st_size, PROT_READ, MAP_FILE | MAP_SHARED, fdin, 0)) == (caddr_t) -1)
    printf("mmap error for input");
  if((dst = mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fdout, 0)) == (caddr_t) -1)
    printf("mmap error for output");
  memcpy(dst, src, statbuf.st_size);
  exit(0);
}


阅读(1557) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~