简体   繁体   English

如何使用 C 语言将目录从一个地方复制到另一个地方?

[英]How to copy the directory from one place to another using C language?

I have searched online but didn't a solution.我在网上搜索过,但没有解决方案。 I want to copy the directories/folders from one place to another using C language.我想使用 C 语言将目录/文件夹从一个地方复制到另一个地方。 Although it could be possible using cp in command line but I want to implement that in C language.虽然可以在命令行中使用cp但我想用 C 语言来实现它。

To copy a folder, including all files and subfolders inside, you need to iterate over the content of the folder, and for each "entry" check if it's a file or folder要复制文件夹,包括其中的所有文件和子文件夹,您需要遍历文件夹的内容,并为每个“条目”检查它是文件还是文件夹

  • if the entry is a file then you can open it as binary and write all the content to a new file in the output path.如果条目是一个文件,那么您可以将其作为二进制文件打开并将所有内容写入 output 路径中的新文件。
  • if the entry is a folder then you need to create the directory in the output path.如果条目是文件夹,则需要在 output 路径中创建目录。

To iterate over the content of the folder you can use readdir from dirent.h , to check if an entry is a folder or file lstat and S_ISDIR from sys/stat.h , and to create folder mkdir from sys/stat.h .要遍历文件夹的内容,您可以使用dirent.h中的readdir ,检查条目是否是文件夹或文件lstatsys/stat.h S_ISDIR sys/stat.hmkdir

This needs some polishing (it ought to handle a full path for a target dir, should think about edge cases, etc.), but this is a reasonable start:这需要一些修饰(它应该处理目标目录的完整路径,应该考虑边缘情况等),但这是一个合理的开始:

/* Recursively duplicate a directory tree */                                       
#include <dirent.h>                                                                
#include <err.h>                                                                   
#include <errno.h>                                                                 
#include <limits.h>                                                                
#include <stdio.h>                                                                 
#include <stdlib.h>                                                                
#include <string.h>                                                                
#include <sys/stat.h>                                                              
#include <unistd.h>                                                                
                                                                                   
static DIR * xopendir(const char *);                                               
static void xclosedir(DIR *, const char *);                                        
static void xstat(const char *, struct stat *);                                    
static void xrealpath(const char *s, char *resolved);                              
static void pathcat(char *p, char *n);                                             
                                                                                   
struct dir { DIR *d; char *path, *end; };                                          
                                                                                   
static void                                                                        
build_dir(char *spath, char *dpath, char *name, mode_t mod)                        
{                                                                                  
        struct dirent *f;                                                          
        struct dir src;                                                            
        struct dir dst;                                                            
                                                                                   
        src.end = strchr(spath, '\0');                                             
        dst.end = strchr(dpath, '\0');                                             
                                                                                   
        pathcat(dpath, name);                                                      
        if( mkdir(dpath, mod) == -1 && errno != EEXIST ){                          
                err(EXIT_FAILURE, "%s", name);                                     
        }                                                                          
                                                                                   
        if( getenv("V") ){                                                         
                printf("%s -> %s\n", spath, dpath);                                
        }                                                                          
        src.d = xopendir(src.path = spath);                                        
                                                                                   
        while( (f = readdir(src.d)) != NULL ) {                                    
                if( !strcmp(f->d_name, ".") || !strcmp(f->d_name, "..") ){         
                        continue;                                                  
                }                                                                  
                if( f->d_type == DT_DIR ){                                         
                        struct stat b;                                             
                        pathcat(spath, f->d_name);                                 
                        xstat(spath, &b);                                          
                        build_dir(spath, dpath, f->d_name, b.st_mode);             
                        *src.end = '\0';                                           
                }                                                                  
        }                                                                          
        *src.end = '\0';                                                           
        *dst.end = '\0';                                                           
        xclosedir(src.d, spath);                                                   
} 
int                                                                                
main(int argc, char **argv)                                                        
{                                                                                  
        if( argc < 2 ){                                                            
                errx(EXIT_FAILURE, "src directory missing");                       
        }                                                                          
        if( argc < 3 ){                                                            
                errx(EXIT_FAILURE, "target directory missing");                    
        }                                                                          
        char spath[PATH_MAX];                                                      
        char dpath[PATH_MAX];                                                      
        xrealpath(argv[1], spath);                                                 
                                                                                   
        for( argv += 2; *argv; argv++ ){                                           
                char *name = *argv;                                                
                if( *name == '/' ){                                                
                        dpath[0] = '\0';                                           
                } else {                                                           
                        getcwd(dpath, sizeof dpath);                               
                }                                                                  
                build_dir(spath, dpath, name, 0777);                               
        }                                                                          
        return 0;                                                                  
}                                                                                  
                                                                                   
static void                                                                        
xrealpath(const char *s, char *resolved)                                           
{                                                                                  
        if( realpath(s, resolved) == NULL ){                                       
                err(EXIT_FAILURE, "%s", s);                                        
        }                                                                          
}                                                                                  
                                                                                   
static DIR *                                                                       
xopendir(const char *path)                                                         
{                                                                                  
        DIR *d = opendir(path);                                                    
        if( d == NULL ) {                                                          
                err(EXIT_FAILURE, "%s", path);                                     
        }                                                                          
        return d;                                                                  
}                                                                                  
                                                                                   
static void                                                                        
xstat(const char *path, struct stat *buf)                                          
{                                                                                  
        if( stat(path, buf) ){                                                     
                err(EXIT_FAILURE, "stat %s", path);                                
        }                                                                          
} 
static void                                                                        
xclosedir(DIR *d, const char *path)                                                
{                                                                                  
        if( closedir(d) ) {                                                        
                err(EXIT_FAILURE, "%s", path);                                     
        }                                                                          
}                                                                                  
                                                                                   
static void                                                                        
pathcat(char *p, char *n)                                                          
{                                                                                  
        if( snprintf(p, PATH_MAX, "%s/%s", p, n) > PATH_MAX - 1 ){                 
                errx(1, "path too long");                                          
        }                                                                          
}  

                                                                            

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在数组中查找一个值并将其复制到 C 语言中的另一个值? - How to find a value in array and copy it to another one in C language? c++ - 如何在c/c++中将文件从一个目录复制到另一个目录 - How can I copy a file from one directory to another in c/c++ 如何使用C语言将图像复制到目录 - How to copy a image to a Directory in C language 如何使用Android中的本机代码将文件从一个目录复制到另一个目录? - How to copy file from one directory to another using native code in Android? C 语言:将数据从一个 typedef 结构复制到另一个并返回一个值 - C language: copy data from one typedef struct to another and return a value 将字符串从一个地方复制到另一个地方 - copy string one place to another 如何使用C语言从目录中的所有文件读取数据? - How to read data from all files in a directory using C Language? C语言-[原位复制] float *到char *,反之亦然 - C Language - [copy in place] float* to char* and the reverse 如何使用C中的指针将一个数组的内容复制到另一个数组? - How to copy contents of one array to another using pointers in C? 如何使用指针算法在C中将值从一个数组复制到另一个数组 - How to copy values from one array to another in C with pointer arithmetic
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM