1.要求文件IO拷贝一张图片
代码如下:
#include<stdio.h> #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<string.h> int main(int argc, const char *argv[]) { int gf = open("./4.png",O_RDONLY); int fg = open("./1.png",O_WRONLY|O_CREAT|O_TRUNC,0664); if(gf<0) { perror("open1"); return-1; } if(fg<0) { perror("open2"); return -1; } char s[20]=""; ssize_t res=0; while(1) { bzero(s,sizeof(s)); res = read(gf,s,sizeof(s)); if(res==0) { break; } write(fg,s,sizeof(s)); } close(gf); close(fg); return 0; }
拷贝成功,效果如下:
2.用标准IO拷贝一份图片
代码如下:
#include<stdio.h>
#include<string.h>
int main(int argc, const char *argv[])
{
FILE *gf = fopen("./1.png","r");
FILE *fg = fopen("./2.png","w+");
if(NULL == gf)
{
perror("fopen1");
return -1;
}
if(NULL == fg)
{
perror("fopen2");
return -1;
}
char s[1];
while(1)
{
bzero(s,sizeof(s));
if(fread(s,1,1,gf) == 0)
{
break;
}
fwrite(s,1,1,fg);
}
fclose(gf);
fclose(fg);
return 0;
}
拷贝成功:
三张图片在文件中如下: