线程拷贝图片;及运用opendir、fopen、readdir、stat等函数拷贝目录所有文件权限及时间至.text文件

拷贝图片现象:

代码:


#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;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值