• 系统编程拷贝文件或者目录(可以做出一个动态库哦)


    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<sys/types.h>
    #include<fcntl.h>
    #include<unistd.h>
    #include<dirent.h>
    #include<sys/stat.h>
    char paths[1000],patht[1000],temp_paths[1000],temp_patht[1000];
    void copy(char *spathname,char *tpathname)//拷贝文件
    {
    int sfp,tfp;
    char buf[1024];
    int count=0;
    struct stat s;
    stat(spathname,&s);
    sfp=open(spathname,O_RDONLY,s.st_mode);//保护原文件权限
    while(1){
    if(sfp<0){printf("don't open the file1 ");exit(1);}
    count=read(sfp,buf,1023);
    if(count==0)break;
    buf[count]='';
    tfp=open(tpathname,O_CREAT|O_RDWR,s.st_mode);
    if(tfp<0){printf("don't open the file2 ");exit(1);}
    write(tfp,buf,strlen(buf));
    }
    close(sfp);
    close(tfp);
    }
    void d_copy(char *spathname,char *tpathname)//拷贝目录
    {
    struct stat s,t,temp_s,temp_t;
    struct dirent *s_p;
    DIR *dirs,*dirt;
    stat(spathname,&s);
    mkdir(tpathname,s.st_mode);
    chown(tpathname,s.st_uid,s.st_gid);
    dirt=opendir(tpathname);
    dirs=opendir(spathname);
    strcpy(temp_paths,spathname);
    strcpy(temp_patht,tpathname);
    while((s_p=readdir(dirs))!=NULL)
    {
    if(strcmp(s_p->d_name,".")!=0&&strcmp(s_p->d_name,"..")!=0)
    {
    strcat(temp_paths,"/");
    strcat(temp_paths,s_p->d_name);
    strcat(temp_patht,"/");
    strcat(temp_patht,s_p->d_name);
    lstat(temp_paths,&s);
    temp_s.st_mode=s.st_mode;
    if(S_ISLNK(temp_s.st_mode))
    {
    printf("%s is a symbol link file ",temp_paths);
    }
    else if(S_ISDIR(temp_s.st_mode))
    {
    printf("copy directory %s.... ",temp_paths);
    d_copy(temp_paths,temp_patht);
    strcpy(temp_paths,spathname);
    strcpy(temp_patht,tpathname);
    }
    else if(S_ISREG(temp_s.st_mode))
    {
    printf("copy file %s.... ",temp_paths);
    copy(temp_paths,temp_patht);
    strcpy(temp_paths,spathname);
    strcpy(temp_patht,tpathname);
    }
    }
    }
    }
    int main(int argc,char *argv[])
    {
    struct dirent *sp,*tp;
    char spath[1000],tpath[1000],temp_spath[1000],temp_tpath[1000];
    struct stat sbuf,tbuf,temp_sbuf,temp_tbuf;
    DIR *dir_s,*dir_t;
    dir_s=opendir(argv[1]);
    if(dir_s==NULL)
    {
    printf("目录不存在");
    return 0;
    }
    if(stat(argv[1],&sbuf)!=0)
    {
    printf("get satus error! ");
    return 0;
    }
    dir_t=opendir(argv[2]);
    if(dir_t==NULL)
    {
    mkdir(argv[2],sbuf.st_mode);
    chown(argv[2],sbuf.st_uid,sbuf.st_gid);
    }
    strcpy(spath,argv[1]);
    strcpy(tpath,argv[2]);
    strcpy(temp_spath,argv[1]);
    strcpy(temp_tpath,argv[2]);
    printf("being copy...");
    while((sp=readdir(dir_s))!=NULL)
    {
    if(strcmp(sp->d_name,".")!=0&&strcmp(sp->d_name,"..")!=0)
    {
    strcat(temp_spath,"/");
    strcat(temp_spath,sp->d_name);
    strcat(temp_tpath,"/");
    strcat(temp_tpath,sp->d_name);
    lstat(temp_spath,&sbuf);
    temp_sbuf.st_mode=sbuf.st_mode;
    if(S_ISLNK(temp_sbuf.st_mode))
    {
    printf("%s is a symbolic link file ",temp_spath);
    }
    else if ((S_IFMT&temp_sbuf.st_mode)==S_IFREG)
    {
    printf("copy file %s..... ",temp_spath);
    copy(temp_spath,temp_tpath);
    strcpy(temp_tpath,tpath);
    strcpy(temp_spath,spath);
    }
    else if((S_IFMT&temp_sbuf.st_mode)==S_IFDIR)
    {
    printf("copy directory %s.... ",temp_spath);
    d_copy(temp_spath,temp_tpath);
    strcpy(temp_tpath,tpath);
    strcpy(temp_spath,spath);
    }

    }
    }
    printf("copy end! ");
    closedir(dir_t);
    closedir(dir_s);
    return 0;

    }

    --欢迎交流哦^-^--

  • 相关阅读:
    airpods2连接win10的方法步骤
    JSON学习笔记
    TCP-IP总线与CAN总线优缺点对比
    svn切换目录
    SQLite学习笔记
    python-opencv安装及入门
    python数据可视化
    python-opencv视觉巡线
    python-opencv进阶应用
    QT窗口和部件
  • 原文地址:https://www.cnblogs.com/eastofeden/p/7375819.html
Copyright © 2020-2023  润新知