1.代码
#include <myhead.h>
struct Info
{
char *srcfile;
char *dstfile;
int start;
int size;
};
int file_len(const char *srcfile)
{
int src,dst;
if((src=open(srcfile,O_RDONLY))==-1)
{
perror("open failed");
return -1;
}
int len=lseek(src,0,SEEK_END);
close(src);
// close(dst);
return len;
}
void *task(void *arg)
{
printf("这是子线程\n");
struct Info info;
info=*(struct Info *)arg;
int src;
int dst;
if((src=open(info.srcfile,O_RDONLY))==-1)
{
perror("open failed");
return NULL;
}
if((dst=open(info.dstfile,O_RDWR|O_CREAT|O_TRUNC,0664))==-1)
{
perror("open failed");
return NULL;
}
lseek(src,info.start,SEEK_SET);
lseek(dst,info.start,SEEK_SET);
int size=file_len(info.srcfile);
char buf[30]="";
int count=0;
while(1)
{
int ret=read(src,buf,sizeof(buf));
count=count+ret;
if(ret==0||count>=size)
{
write(dst,buf,ret-(count-size));
break;
}
write(dst,buf,ret);
}
close(src);
close(dst);
}
int main(int argc, char *argv[])
{
if(argc!=3)
{
printf("input file wrong\n");
printf("please input the right file\n");
return -1;
}
int len;
len=file_len(argv[1]);
pthread_t pid;
struct Info info[2]={{argv[1],argv[2],0,len/2},{argv[1],argv[2],len/2,len-len/2}};
if(pthread_create(&pid,NULL,task,&info[0]))
{
perror("creat s1 error\n");
return -1;
}
if(pthread_create(&pid,NULL,task,&info[1]))
{
perror("creat s2 error\n");
return -1;
}
printf("这是主线程\n");
while(1);
return 0;
}
2.实现界面