📄 p10-12.c
字号:
#include <sys/types.h>#include <sys/stat.h>#include <sys/mman.h> /* mmap ()*/#include <fcntl.h>#include <stdio.h>#define FILE_MODE 0666int main(int argc, char *argv[] ){ int fdfrom , fdto; char *src, *dst; struct stat statbuf1,statbuf2; if (argc != 3) printf( "usage: a.out <fromfile> <tofile>\n" ); /* 打开要连接的二个文件 */ if ((fdfrom = open(argv[1], O_RDONLY)) < 0){ printf("can't open %s for reading", argv[1]); exit(1); } if ((fdto = open(argv[2], O_RDWR|O_CREAT, FILE_MODE)) < 0){ printf("can't creat %s for writing", argv[2]); exit(1); } /* 获取这二个文件的大小*/ if (fstat(fdfrom, &statbuf1) < 0){ /* need size of file1 */ printf("fstat errort") ; exit(1); } if (fstat(fdto, &statbuf2) < 0){ /* need size of file2 */ printf("fstat errort") ; exit(1); } /* 扩充输出文件的大小使之等于这二个文件大小之和 */ if (lseek(fdto, statbuf1.st_size - 1, SEEK_END) == -1){ printf("lseek error"); exit(1); } /* 在文件尾部写一字符使扩充有效 */ if (write (fdto, "", 1) != 1){ printf("write error") ; exit(1); } /* 映射这二个文件至内存 */ if ((src = mmap(0, statbuf1.st_size, PROT_READ, MAP_SHARED, fdfrom, 0)) == (caddr_t)-1){ printf("mmap error for input") ; exit(1); } if ((dst = mmap(0, statbuf2.st_size + statbuf1.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fdto, 0)) == (caddr_t)-1){ printf("mmap error for output"); exit(1); } /* 连接第一个文件至第二个文件 */ memcpy( dst+statbuf2.st_size, src, statbuf1.st_size); exit(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -