如何检查目录是否存在?

时间:2021-07-26 00:03:36

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

如何检查Linux中是否存在一个目录?

7 个解决方案

#1


47  

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

您可以使用opendir()并检查ENOENT == errno在失败时:

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. */
}

#2


25  

Use the following code to check if a folder exists. It works on both Windows & Linux platforms.

使用以下代码检查文件夹是否存在。它同时适用于Windows和Linux平台。

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

int main(int argc, char* argv[])
{
    const char* folderr;
    //folderr = "C:\\Users\\SaMaN\\Desktop\\Ppln";
    folderr = "/tmp";
    struct stat sb;

    if (stat(folderr, &sb) == 0 && S_ISDIR(sb.st_mode))
    {
        printf("YES\n");
    }
    else
    {
        printf("NO\n");
    }
}

#3


13  

You might use stat() and pass it the address of a struct stat, then check its member st_mode for having S_IFDIR set.

您可以使用stat()并将struct stat的地址传递给它,然后检查它的成员st_mode是否设置了S_IFDIR。

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

...

char d[] = "mydir";

struct stat s = {0};

if (!stat(d, &s))
  printf("'%s' is %sa directory.\n", d, (s.st_mode & S_IFDIR)  : "" ? "not ");
  // (s.st_mode & S_IFDIR) can be replaced with S_ISDIR(s.st_mode)
else
  perror("stat()");

#4


8  

The best way is probably trying to open it, using just opendir() for instance.

最好的方法可能是尝试打开它,例如使用opendir()。

Note that it's always best to try to use a filesystem resource, and handling any errors occuring because it doesn't exist, rather than just checking and then later trying. There is an obvious race condition in the latter approach.

请注意,最好尝试使用文件系统资源,并处理出现的任何错误,因为它不存在,而不是只是检查然后稍后再尝试。后一种方法存在明显的竞争条件。

#5


4  

According to man(2)stat you can use the S_ISDIR macro on the st_mode field:

根据man(2)stat可以在st_mode字段中使用S_ISDIR宏:

bool isdir = S_ISDIR(st.st_mode);

Side note, I would recommend using Boost and/or Qt4 to make cross-platform support easier if your software can be viable on other OSs.

另外,我建议使用Boost和/或Qt4来简化跨平台支持,如果您的软件可以在其他OSs上使用的话。

#6


1  

You may also use access in combination with opendir to determine if the directory exists, and, if the name exists, but is not a directory. For example:

您还可以结合使用access与opendir确定该目录是否存在,以及该名称是否存在,但不是目录。例如:

/* test that dir exists (1 success, -1 does not exist, -2 not dir) */
int
xis_dir (char *d)
{
    DIR *dirptr;

    if (access ( d, F_OK ) != -1 ) {
        // file exists
        if ((dirptr = opendir (d)) != NULL) {
            closedir (dirptr);
        } else {
            return -2; /* d exists, but not dir */
        }
    } else {
        return -1;     /* d does not exist */
    }

    return 1;
}

#7


-5  

Another two ways, maybe less correct is to use. The first one, using only standard libraries and for files only:

另外两种方法,也许不太正确的是使用。第一个,只使用标准库和文件:

FILE *f;
f = fopen("file", "r")
if(!f)
   printf("there is no file there");

This one might work on all the OS.

这个可能适用于所有操作系统。

Or another also for dirs, using the system call with system(). Is the worst option, but gives you another way. For someone maybe useful.

或者另一个用于dirs,使用system()的系统调用。是最坏的选择,但给你另一种方法。对于某人来说也许有用。

#1


47  

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

您可以使用opendir()并检查ENOENT == errno在失败时:

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. */
}

#2


25  

Use the following code to check if a folder exists. It works on both Windows & Linux platforms.

使用以下代码检查文件夹是否存在。它同时适用于Windows和Linux平台。

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

int main(int argc, char* argv[])
{
    const char* folderr;
    //folderr = "C:\\Users\\SaMaN\\Desktop\\Ppln";
    folderr = "/tmp";
    struct stat sb;

    if (stat(folderr, &sb) == 0 && S_ISDIR(sb.st_mode))
    {
        printf("YES\n");
    }
    else
    {
        printf("NO\n");
    }
}

#3


13  

You might use stat() and pass it the address of a struct stat, then check its member st_mode for having S_IFDIR set.

您可以使用stat()并将struct stat的地址传递给它,然后检查它的成员st_mode是否设置了S_IFDIR。

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

...

char d[] = "mydir";

struct stat s = {0};

if (!stat(d, &s))
  printf("'%s' is %sa directory.\n", d, (s.st_mode & S_IFDIR)  : "" ? "not ");
  // (s.st_mode & S_IFDIR) can be replaced with S_ISDIR(s.st_mode)
else
  perror("stat()");

#4


8  

The best way is probably trying to open it, using just opendir() for instance.

最好的方法可能是尝试打开它,例如使用opendir()。

Note that it's always best to try to use a filesystem resource, and handling any errors occuring because it doesn't exist, rather than just checking and then later trying. There is an obvious race condition in the latter approach.

请注意,最好尝试使用文件系统资源,并处理出现的任何错误,因为它不存在,而不是只是检查然后稍后再尝试。后一种方法存在明显的竞争条件。

#5


4  

According to man(2)stat you can use the S_ISDIR macro on the st_mode field:

根据man(2)stat可以在st_mode字段中使用S_ISDIR宏:

bool isdir = S_ISDIR(st.st_mode);

Side note, I would recommend using Boost and/or Qt4 to make cross-platform support easier if your software can be viable on other OSs.

另外,我建议使用Boost和/或Qt4来简化跨平台支持,如果您的软件可以在其他OSs上使用的话。

#6


1  

You may also use access in combination with opendir to determine if the directory exists, and, if the name exists, but is not a directory. For example:

您还可以结合使用access与opendir确定该目录是否存在,以及该名称是否存在,但不是目录。例如:

/* test that dir exists (1 success, -1 does not exist, -2 not dir) */
int
xis_dir (char *d)
{
    DIR *dirptr;

    if (access ( d, F_OK ) != -1 ) {
        // file exists
        if ((dirptr = opendir (d)) != NULL) {
            closedir (dirptr);
        } else {
            return -2; /* d exists, but not dir */
        }
    } else {
        return -1;     /* d does not exist */
    }

    return 1;
}

#7


-5  

Another two ways, maybe less correct is to use. The first one, using only standard libraries and for files only:

另外两种方法,也许不太正确的是使用。第一个,只使用标准库和文件:

FILE *f;
f = fopen("file", "r")
if(!f)
   printf("there is no file there");

This one might work on all the OS.

这个可能适用于所有操作系统。

Or another also for dirs, using the system call with system(). Is the worst option, but gives you another way. For someone maybe useful.

或者另一个用于dirs,使用system()的系统调用。是最坏的选择,但给你另一种方法。对于某人来说也许有用。