首页 > 解决方案 > 如何写入目录C

问题描述

我正在尝试将目录内容复制到另一个目录,但是在不使用sprintf;的情况下写入第二个目录时遇到问题 有什么建议么?

void Copying(char *folder1, char *folder2) {
    DIR *s1, *s2;
    struct dirent *dep;

    if ((s1 = opendir(folder1)) == NULL) {
        printf("Error\n");
        return;
    }
    if ((s2 = opendir(folder2)) == NULL) {
        printf("Error \n");
        return;
    }

    while ((dep = readdir(s1)) != NULL) {
        //write(s2 , dep->d_name , sizeof(dep) ) ; // <- 
    }
    closedir(s1);
    closedir(s2);

    return;
}

标签: c

解决方案


这种方法行不通。目录句柄不可写。

如果要复制目录的内容,则需要分别复制源目录中的每个文件、目录和链接。要复制每个文件,您需要在目标目录中创建一个新文件并将源文件的内容写入其中。


推荐阅读