#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);
}
阅读(1606) | 评论(0) | 转发(0) |