在C新建并使用目录:无法在子目录中打开文件

时间:2013-09-13 00:56:08

标签: c directory

我第一次使用目录并遇到了一些困难。我编写了以下函数来探索目录,显示文件大小和权限,然后递归到任何子目录。

void exploreDIR (DIR* dir, char cwd[], int tab)
{

    struct dirent* ent;

    while ((ent = readdir(dir)) != NULL)
    {

        if(strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
             continue;

        int i;
        i = 0;
        while(i < tab)
        {
            printf("    ");
            i = i + 1;
        }

        printf("%s ", ent->d_name);
        FILE *file = fopen(ent->d_name, "r");

        int filesize;

        if(file==NULL)
        {
            printf("[ Could not open! ]\n");
            continue; 
        }              

        struct stat st;
        stat(ent->d_name, &st);
        filesize = st.st_size;                                       

        if (st.st_mode & S_IFDIR)
        {
            printf ("(subdirectory) [");
        }
        else
        {
            printf("(%d bytes) [", filesize);
        }

        printf( (S_ISDIR(st.st_mode)) ? "d" : "-");
        printf( (st.st_mode & S_IRUSR) ? "r" : "-");
        printf( (st.st_mode & S_IWUSR) ? "w" : "-");
        printf( (st.st_mode & S_IXUSR) ? "x" : "-");
        printf( (st.st_mode & S_IRGRP) ? "r" : "-");
        printf( (st.st_mode & S_IWGRP) ? "w" : "-");
        printf( (st.st_mode & S_IXGRP) ? "x" : "-");
        printf( (st.st_mode & S_IROTH) ? "r" : "-");
        printf( (st.st_mode & S_IWOTH) ? "w" : "-");
        printf( (st.st_mode & S_IXOTH) ? "x" : "-");
        printf("]\n");

        fclose(file);

        if (st.st_mode & S_IFDIR)
        {
             char tempwd[1024];
             strcpy(tempwd, cwd);
             strcat(tempwd, "/");
             strcat(tempwd, ent->d_name);
             DIR* tempdir;
             if ((tempdir = opendir (tempwd)) != NULL)
             {
                 printf("%s", tempwd);
                 exploreDIR(tempdir, tempwd, tab + 1);               
             }

         }
    }
    closedir(dir); 
}

但是当函数在子目录上进行递归时,fopen函数总是返回null。我无法想象我的生活。作为参考,这是主要的:

int main(int argc, char** argv) 
{
    printf("\n");

    DIR* dir;   
    char cwd[1024];
    if (getcwd(cwd, sizeof(cwd)) != NULL)
    {
        if ((dir = opendir (cwd)) != NULL)
        {
            exploreDIR(dir, cwd, 0);               
        }
    }    

    printf("\n");
    return (EXIT_SUCCESS);
}

我也有点担心我的方法。 strcat()真的是探索子目录的最佳方式吗?

由于

2 个答案:

答案 0 :(得分:2)

您似乎没有使用cwd变量。你应该把它放在每个文件名的开头。

此外,我喜欢使用snprintf而不是strcat来构建名称。它将自己限制为您传递的缓冲区大小,因此不存在溢出的风险。

答案 1 :(得分:2)

当您打开子目录./snails时,您从readdir()目录中读取文件名,例如chewy-snails,但是stat()文件,您需要使用子目录名称为名称添加前缀:./snails/chewy-snails

替代方案,不要轻易采取,是考虑使用chdir()或(更好)fchdir()将目录更改为相关的子目录。然后,您可以stat() readdir() *at()中的名称而不会弄乱字符串。但是,回到你开始的地方是一个问题 - 特别是如果你按照符号链接到达那里。这是fchdir()得分的地方。

如果您使用的是具有必要支持的系统,则{{1}}功能可能有所帮助(fstatat()等)。

但最简单的解决方案是在尝试使用stat()之前不更改目录并使用子目录名称为文件名添加前缀或 lstat() - 这是指向相同内容的3个链接。