主函数
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
int fd_r = open(argv[1],O_RDONLY);
if (fd_r < 0)
{
perror("open_fd");
return -1;
}
printf("open success\n");
umask(0);
int fd_w = open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,0664);
if (fd_w < 0)
{
perror("open_fd1");
return -1;
}
char medi;
while (1)
{
int res = read(fd_r,&medi,1);
if (res < 1)
{
perror("read");
return -1;
}
int res1 = write(fd_w,&medi,1);
if (res1 < 0)
{
perror("write");
return -1;
}
}
if(close(fd_r) < 0)
{
perror("read_close");
return -1;
}
if(close(fd_w) < 0)
{
perror("write_close");
return -1;
}
printf("close success\n");
return 0;
}
测试
root@VM-12-9-ubuntu:zouye# a any.jpg test.jpg
open success
read: Success
原图片
copy成功的图片