#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
/******用read和write拷贝一张图片*******/
long int res = open(argv[1], O_RDONLY);
if(res < 0)
{
perror("res_open");
return -1;
}
printf("图片打开成功!\n");
long int res1 = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0777);
if(res1 < 0)
{
perror("res1_open");
return -1;
}
char c;
while(1)
{
int a = read(res, &c, 1);
if(a < 1)
{
perror("read");
return -1;
}
int b = write(res1, &c, 1);
if(b < 0)
{
perror("write");
return -1;
}
}
close(res);
close(res1);
printf("图片复制完成!\n");
return 0;
}
测试结果: