1.数组
#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#include<string.h>
char buf[]="1234567";
void*fun1(void*buf)
{
for(int i=0;i<10;i++)
{
printf("线程1=%s\n",(char*)buf);
}
}
void *fun2(void*buf)
{
int len=strlen(buf)-1;
for(int j=0;j<len/2;j++)
{
char temp;
temp=((char*) buf)[j];
((char*) buf)[j]=((char*)buf)[len-j];
((char*)buf)[len-j]=temp;
}
}
int main(int argc, const char *argv[])
{
pthread_t pth_A;
pthread_t pth_B;
pthread_create(&pth_A,NULL,fun1,(void*)buf);
pthread_create(&pth_A,NULL,fun2,(void*)buf);
while(1);
return 0;
}
2 复制图
#include<stdio.h>
#include<pthread.h>
#include<errno.h>
#include<unistd.h>
void* FUNC(void*i)
{
FILE*fp=fopen("./1.png","r");
FILE*my_fp=fopen("my.png","a+");
fseek(fp,*(long*)i,SEEK_SET);
size_t P;
char date;
while(1)
{
P=fread(&date,1,sizeof(date),fp);
fwrite(&date,1,1,my_fp);
if(P==0)
{
printf("读取完毕\n");
break;
}
}
}
int main(int argc, const char *argv[])
{
FILE*fp=fopen("./1.png","r");
if(NULL==fp)
{
perror("fp");
return -1;
}
FILE*my_fp=fopen("my.png","w");
if(NULL==my_fp)
{
perror("my_fp");
return -1;
}
//读取前半
long LEN;
fseek(fp,0,SEEK_END);
LEN=ftell(fp);
fseek(fp,0,SEEK_SET);
char date;
int PP;
long i=0;
while(i<LEN/2)
{
PP=fscanf(fp,"%c",&date);
fprintf(my_fp,"%c",date);
if(PP==EOF)
{
if(errno!=0)
{
perror("fscanf");
return -1;
}
printf("复制完成\n");
break;
}
i++;
}
fclose(fp);
fclose(my_fp);
//使用fread;________________
/* size_t P;
* P=fread(&date,1,sizeof(date),fp);
fwrite(&date,1,1,my_fp);
if(P==0)
{
printf("读取完成\n");
break;
}*/
//开线程——————————————————————————————————————
pthread_t my_LINK;
pthread_create(&my_LINK,NULL,FUNC,(void*)&i);
pthread_join(my_LINK,NULL);
fclose(fp);
fclose(my_fp);
return 0;
}