检查路径是文件还是目录

时间:2013-12-17 02:56:28

标签: c

我刚刚开始学习C并尝试进行路径列表。我尝试使用dirent列出目录中的路径,并尝试使用stat检查结果是文件还是目录。但是,即使路径是文件,它也会将每个路径作为目录返回。

这是我的代码:
[编辑]

    #include <stdio.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <dirent.h>

    int main(void)
    {
        DIR *mydir = opendir("/Library/Logs");
        char path[250];
        struct dirent *entry = NULL;
        struct stat buf;

        while((entry = readdir(mydir))) /* If we get EOF, the expression is 0 and
                                         * the loop stops. */
        {
            snprintf(path, 250, "%s",entry->d_name);
            stat(path,&buf);
            if(S_ISDIR(buf.st_mode))
                printf("D: %s\n", path);
            else if (S_ISREG(buf.st_mode))
                printf("F: %s\n", path);
            else
                printf("O: %s\n", path);
        }
}

2 个答案:

答案 0 :(得分:3)

stat(entry->d_name,&buf);

此时,您没有关于您正在查找的目录的任何上下文。

您需要创建一个缓冲区,并在调用directory之前连接filename / stat(使用strcatsnprintf

检查对stat的返回值的调用 - 如果非零,请查看errno以查看出现了什么问题。我猜它现在正在报告ENOENT。

答案 1 :(得分:0)

尝试使用 readdir dirent

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
/* "readdir" etc. are defined here. */
#include <dirent.h>
/* limits.h defines "PATH_MAX". */
#include <limits.h>

/* List the files in "dir_name". */

static void
list_dir (const char * dir_name)
{
    DIR * d;

    /* Open the directory specified by "dir_name". */

    d = opendir (dir_name);

    /* Check it was opened. */
    if (! d) {
        fprintf (stderr, "Cannot open directory '%s': %s\n",
                 dir_name, strerror (errno));
        exit (EXIT_FAILURE);
    }
    while (1) {
        struct dirent * entry;
        const char * d_name;

        /* "Readdir" gets subsequent entries from "d". */
        entry = readdir (d);
        if (! entry) {
            /* There are no more entries in this directory, so break
               out of the while loop. */
            break;
        }
        d_name = entry->d_name;
        /* Print the name of the file and directory. */
        printf ("%s/%s\n", dir_name, d_name);

        /* See if "entry" is a subdirectory of "d". */

        if (entry->d_type & DT_DIR) {

            /* Check that the directory is not "d" or d's parent. */

            if (strcmp (d_name, "..") != 0 &&
                strcmp (d_name, ".") != 0) {
                int path_length;
                char path[PATH_MAX];

                path_length = snprintf (path, PATH_MAX,
                                        "%s/%s", dir_name, d_name);
                printf ("%s\n", path);
                if (path_length >= PATH_MAX) {
                    fprintf (stderr, "Path length has got too long.\n");
                    exit (EXIT_FAILURE);
                }
                /* Recursively call "list_dir" with the new path. */
                list_dir (path);
            }
        }
    }
    /* After going through all the entries, close the directory. */
    if (closedir (d)) {
        fprintf (stderr, "Could not close '%s': %s\n",
                 dir_name, strerror (errno));
        exit (EXIT_FAILURE);
    }
}

int main ()
{
    list_dir ("/Library/Logs");
    return 0;
}

输出

/Library/Logs/.<br />
/Library/Logs/..<br />
/Library/Logs/somedir001<br />
/Library/Logs/somedir002