用read write拷贝一张图片
图片就是二进制的普通文件。
diff 1.png 2.png 比较
eog 1.png 打开图片。或者到图形化界面,双击图片
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(int argc, const char *argv[])
{
if(argc < 3){
printf("请输出复制的图片名字\n");
return -1;
}
int fp1 = open(argv[1], O_RDONLY);
if(-1 == fp1){
perror("open");
return -1;
}
int fp2 = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0777);
if(-1 == fp2){
perror("open");
return -1;
}
char buf[32];
ssize_t res = 0;
while((res = read(fp1, buf, sizeof(buf))) > 0){
write(fp2, buf, res);
memset(buf, 0, sizeof(buf));
}
if(-1 == res){
perror("read");
return -1;
}
close(fp1);
close(fp2);
return 0;
}