在Linux中,您可以使用copendir()
函数从给定目录中复制所有子目录
#include#include #include #include int copendir(const char *src, const char *dest) { DIR *src_dir, *dest_dir; struct dirent *entry; struct stat statbuf; char path[PATH_MAX]; // 打开源目录 if ((src_dir = opendir(src)) == NULL) { perror("opendir"); return -1; } // 创建目标目录 if (mkdir(dest, 0777) != 0) { perror("mkdir"); closedir(src_dir); return -1; } // 复制源目录中的所有子目录 while ((entry = readdir(src_dir)) != NULL) { if (entry->d_type == DT_DIR) { snprintf(path, sizeof(path), "%s/%s", src, entry->d_name); if (copendir(path, dest) == -1) { perror("copendir"); closedir(src_dir); return -1; } } } // 关闭源目录 closedir(src_dir); return 0; } int main() { const char *src = "https://www.yisu.com/path/to/source"; const char *dest = "/path/to/destination"; if (copendir(src, dest) == 0) { printf("Directories copied successfully.\n"); } else { printf("Error copying directories.\n"); } return 0; }
这个程序首先打开源目录,然后创建目标目录。接下来,它遍历源目录中的所有子目录,并使用copendir()
函数递归地复制它们到目标目录。最后,它关闭源目录。
请注意,您需要将/path/to/source
和/path/to/destination
替换为实际的源和目标目录路径。