#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
int a=open("1.png",O_RDWR);
int b=open("2.png",O_RDWR|O_CREAT,0777);
if(a<0)
{
printf("文件打开失败\n");
return -1;
}
char arr[32]="";
ssize_t res=0;
while(1)
{
res=read(a,arr,sizeof(arr));
if(res<0)
{
perror("read");
break;
}
else if(res==0)
{
printf("文件读取完毕\n");
return -1;
}
write(b,arr,res);
// printf("%s",arr);
}
if(close(a)<0)
{
printf("文件关闭失败\n");
return -1;
}
return 0;
}
运行结果