1>多线程实现文件拷问
代码:
#define MAXSIZE 1024
#include<myhead.h>
int main(int argc, char const *argv[])
{
FILE *srcfp=NULL;
FILE *destfp=NULL;
if((srcfp = fopen("./pm.bmp", "r+")) == NULL)
{
perror("fopen error");
return -1;
}
//定义变量接收大小
int bmpSize = 0;
//将光标后移2字节
fseek(srcfp, 2, SEEK_SET);
//读取图像的大小
fread(&bmpSize, sizeof(int), 1, srcfp);
fclose(srcfp);
if(!(srcfp=fopen("pm.bmp","r")))
PRINT_ERR("");
if(!(destfp=fopen("pm1.bmp","w")))
PRINT_ERR("");
char buf[MAXSIZE];
int ret=0;
pid_t pid =fork();
if(pid>0){
while(1){
int temp=bmpSize/2-ftell(srcfp);
if(temp>0&&temp<sizeof(buf)){
ret = fread(buf, 1, temp, srcfp);
fwrite(buf, 1, ret, destfp);
break;
}
ret = fread(buf, 1, sizeof(buf), srcfp);
fwrite(buf, 1, ret, destfp);
}
}
else if (pid==0){
fseek(srcfp,bmpSize/2,SEEK_SET);
while((ret = fread(buf, 1, sizeof(buf), srcfp))!= 0)
fwrite(buf, 1, ret, destfp);
exit(EXIT_SUCCESS);
}
else
PRINT_ERR("");
//wait(NULL);
fclose(srcfp);
fclose(destfp);
return 0;
}
结果: