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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12510874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:24:22  来源:igfitidea点击:

How can I check if a directory exists?

clinuxdirectory

提问by user1543915

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

如何用 C 语言检查 Linux 上是否存在目录?

采纳答案by hmjd

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

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

#include <dirent.h>
#include <errno.h>

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

回答by unwind

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

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

Note that it's always best to try to usea 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.

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

回答by alk

You might use stat()and pass it the address of a struct stat, then check its member st_modefor having S_IFDIRset.

您可以使用stat()并将 a 的地址传递给它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()");

回答by TheFriendlyDragon

According to man(2)statyou 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 来简化跨平台支持。

回答by Saman

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* folder;
    //folder = "C:\Users\SaMaN\Desktop\Ppln";
    folder = "/tmp";
    struct stat sb;

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

回答by David C. Rankin

You may also use accessin combination with opendirto 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 (const 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;
}