1.用文件IO函数拷贝一个图片
#include <head.h>
#include <stdlib.h>
#include <sys/wait.h>
int main(int argc, const char *argv[])
{
//打开文件
int f=open("./1.png",O_RDWR);
//打开目标文件
int fp=open("./2.png",O_WRONLY);
//计算文件大小
off_t offset=lseek(f,0,SEEK_END);
//创建子进程
pid_t pid=fork();
if(pid>0)
{
//父进程
char c=0;
lseek(f,0,SEEK_SET);
lseek(fp,0,SEEK_SET);
for(int i=0;i<offset/2;i++)
{
read(f,&c,sizeof(c));
write(fp,&c,sizeof(c));
}
printf("前\n");
wait(NULL);
}
else if(pid == 0)
{
lseek(f,offset/2,SEEK_SET);
lseek(fp,offset/2,SEEK_SET);
char c=0;
for(int i=offset/2;i<offset;i++)
{
read(f,&c,sizeof(c));
write(fp,&c,sizeof(c));
}
printf("后\n");
}
else if(pid<0)
{
return -1;
}
close(f);
close(fp);
return 0;
}