#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<dirent.h>
#include<pthread.h>
typedef struct
{
int len;
FILE* str;
FILE* des;
}*filecopy;
void* pthread1(void* args)
{
filecopy data=(filecopy)args;
fseek(data->str,0,SEEK_SET);
fseek(data->des,0,SEEK_SET);
int i=0;char ch;
while((ch=fgetc(data->str))!=EOF)
{
fputc(ch,data->des);
i++;
if(i>(data->len))
{
break;
}
}
}
void* pthread2(void* args)
{
filecopy data=(filecopy)args;
fseek(data->str,data->len,SEEK_SET);
fseek(data->des,data->len,SEEK_SET);
char ch;
while((ch=fgetc(data->str))!=EOF)
{
fputc(ch,data->des);
}
}
int main(int argc, const char *argv[])
{
if(argc!=3)
{
printf("./a.out <str> <des>");
return -1;
}
filecopy data;
data->str=fopen(argv[1],"r");
if(data->str==NULL)
{
perror("open error");
return -1;
}
data->des=fopen(argv[2],"w");
fseek(data->str,0,SEEK_END);
data->len=ftell(data->str)/2;
pthread_t tid1,tid2;
if(pthread_create(&tid1,NULL,pthread1,(void*)data))
{
perror("tid1 error");
return -1;
}
if(pthread_create(&tid2,NULL,pthread2,(void*)data))
{
perror("tid2 error");
return -1;
}
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
return 0;
}