user1543915 user1543915 - 1 month ago 24
C Question

How can I check if a directory exists?

How can I check if a directory exists on Linux in C?

Answer

You can use opendir() and check if ENOENT == errno on failure:

DIR* dir = opendir("mydir");
if (dir)
{
    /* Directory exists. */
    closedir(dir);
}
else if (ENOENT == errno)
{
    /* Directory does not exist. */
}
else
{
    /* opendir() failed for some other reason. */
}