Linux系统编程——使用 read 和 write 实现拷贝文件

江枫思渺然 提交于 2019-12-18 21:53:22
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>

#define SIZE 8192

int main(int argc, char *argv[]) {
    char buf[SIZE];
    int fd_src, fd_dest, len;

    if(argc < 3) {
        printf("usage: ./mycp src dest\n");
        exit(1);
    }

    fd_src = open(argv[1], O_RDONLY);
    fd_dest = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC, 0664);

    while(len = read(fd_src, buf, sizeof(buf))) {
        write(fd_dest, buf, len);
    }

    close(fd_src);
    close(fd_dest);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!