拷贝图片现象:
代码:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
void* thread01(void* arg)
{
printf("this is thread01 func...\n");
int pa = open("./IMG20240608195947.jpg",O_RDONLY);
int pb = open("1.jpg",O_WRONLY | O_CREAT |O_TRUNC,0777);
if(pa < 0 || pb < 0)
{
perror("open");
return NULL;
}
char buf;
size_t t = sizeof(buf);
ssize_t re;
int i;
off_t L = lseek(pa,0,SEEK_END);
lseek(pb,0,SEEK_SET);
lseek(pa,0,SEEK_SET);
for(i = 0;i < L/2; i++)
{
buf = '0';
re = read(pa,&buf,t);
if(re < 0)
{
perror("read");
return NULL;
}
write(pb,&buf,t);
}
static int a = 1;
pthread_exit((void*)&a);
return NULL;
}
void* thread02(void* arg)
{
printf("this is thread02 func...\n");
int pa = open("./IMG20240608195947.jpg",O_RDONLY);
int pb = open("1.jpg",O_WRONLY,0777);
if(pa < 0 || pb < 0)
{
perror("open");
return NULL;
}
char buf;
size_t t = sizeof(buf);
ssize_t re;
int i;
off_t L = lseek(pa,0,SEEK_END);
lseek(pb,L/2,SEEK_SET);
lseek(pa,L/2,SEEK_SET);
for(i = L/2;i < L; i++)
{
buf = '0';
re = read(pa,&buf,t);
if(re < 0)
{
perror("read");
return NULL;
}
write(pb,&buf,t);
}
close(pa);
close(pb);
static int b = 2;
pthread_exit((void*)&b);
return NULL;
}
int main(int argc, const char* argv[])
{
pthread_t A;
if (pthread_create(&A, NULL, thread01, NULL) != 0)
{
fprintf(stderr, "pthread_create failed __%d__\n", __LINE__);
return -1;
}
void* ptr;
pthread_join(A, &ptr);
putchar(10);
pthread_t B;
if (*(int*)ptr == 1)
{
//pthread_t B;
if (pthread_create(&B, NULL, thread02, NULL) != 0)
{
fprintf(stderr, "pthread_create failed __%d__\n", __LINE__);
return -1;
}
}
void* ptr2;
pthread_join(B, &ptr2);
printf("\nthis is main func...\n");
return 0;
}
写入.text文件现象:
代码:
// 权限及时间拷贝
int main(int argc, const char *argv[])
{
DIR*op = opendir("../cpyjpg/");
//int f = open("./information.txt",O_WRONLY | O_CREAT | O_TRUNC,0777);
FILE*f = fopen("./information.txt","w+");
if(op == NULL)
{
perror("opendir");
return -1;
}
struct dirent*re;
int i = 0;
re = readdir(op);
//printf("%s\n",re->d_name);
struct stat buf;
struct tm*time;
while(1)
{
re = readdir(op);
if(re == NULL)
{
if(errno == 0)break;
else{perror("readdir");return -1;}
}
if(re->d_name[0] != '.')
{
stat(re->d_name,&buf);
time = localtime(&buf.st_atime);
fprintf(f,"[%d]%s:%o,%d-%02d-%02d %02d:%02d:%02d\n",++i,re->d_name,buf.st_mode,
time->tm_year+1900,time->tm_mon+1,time->tm_mday,
time->tm_hour,time->tm_min,time->tm_sec);
//printf("[%d]%s\n",++i,re->d_name);
}
}
closedir(op);
return 0;
}