简体   繁体   中英

Checking whether path is a file or directory

I just started learning C and am trying to do path listing. I have tried to list the path in the directory using dirent and tried to check if the result is a file or directory, using stat . However, even if the path is a file, it will return every path as a directory.

This is my code:
[Edit]

    #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);
        }
}
stat(entry->d_name,&buf);

At this point, you don't have any context about the directory you're looking in.

You will need to create a buffer, and concatenate your directory / filename before calling stat (using strcat or snprintf )

Checking the call to stat 's return value -- if non-zero, look at errno to see what went wrong. I'm guessing it's currently reporting an ENOENT.

Try this, using readdir and 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;
}

The output will be

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM