Linux系统函数_read和write实现文件拷贝功能 代码
Linux系统函数_read和write实现文件拷贝功能 代码,练习所写不足之处请在下方评论指出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <stdlib.h> #include <errno.h> #define N 1024 int main(int argc, char const* argv[]) { int fd, fd_out; int n; char buf[N]; fd = open(argv[1], O_RDONLY); if(fd < 0) { perror("open 1 error"); exit(1); } fd_out = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0644); if(fd_out < 0) { perror("open 2 error"); exit(1); } while((n = read(fd, buf, N))) { if(n < 0) { perror("read error"); exit(1); } write(fd_out, buf, n); } close(fd); close(fd_out); return 0; } |