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

C 如何检查目录是否存在?,c,linux,directory,C,Linux,Directory,如何在C中检查Linux上是否存在目录?最好的方法可能是尝试打开它,例如仅使用 请注意,最好总是尝试使用文件系统资源,并处理因其不存在而发生的任何错误,而不是只是检查然后稍后重试。在后一种方法中存在明显的争用条件。您可以使用并检查失败时的enoint==errno: #include <dirent.h> #include <errno.h> DIR* dir = opendir("mydir"); if (dir) { /* Directory exists.

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

最好的方法可能是尝试打开它,例如仅使用

请注意,最好总是尝试使用文件系统资源,并处理因其不存在而发生的任何错误,而不是只是检查然后稍后重试。在后一种方法中存在明显的争用条件。

您可以使用并检查失败时的
enoint==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. */
}
#包括
#包括
DIR*DIR=opendir(“mydir”);
if(dir){
/*目录存在*/
closedir(dir);
}else if(enoint==errno){
/*目录不存在*/
}否则{
/*opendir()由于其他原因失败*/
}
您可以使用
stat()
并将
struct stat
的地址传递给它,然后检查其成员
st\u mode
是否设置了
S\u 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()");
#包括
#包括
#包括
#包括
...
字符d[]=“mydir”;
struct stat s={0};
如果(!统计数据(d和s))
printf(“%s”是%sa目录。\n”,d,(s.st_mode&s_IFDIR):“?”不是“;
//(s.st_模式和s_IFDIR)可替换为s_ISDIR(s.st_模式)
其他的
perror(“stat()”);
根据需要,您可以在st_模式字段中使用S_ISDIR宏:

bool isdir = S_ISDIR(st.st_mode);

附带说明,如果您的软件可以在其他操作系统上运行,我建议使用Boost和/或Qt4来简化跨平台支持。

使用以下代码检查文件夹是否存在。它可以在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");
    }
}
#包括
#包括
int main(int argc,char*argv[])
{
const char*文件夹;
//folder=“C:\\Users\\SaMaN\\Desktop\\Ppln”;
folder=“/tmp”;
结构统计某人;
if(stat(folder,&sb)==0&S_ISDIR(sb.st_模式)){
printf(“是\n”);
}否则{
printf(“否”);
}
}

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

#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>

/* 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); /* d exists and is a directory */
        } else {
            return -2; /* d exists but is not a directory */
        }
    } else {
        return -1;     /* d does not exist */
    }

    return 1;
}
#包括
#包括
#包括
/*测试目录是否存在(1成功,-1不存在,-2不存在目录)*/
int
xis_dir(常量字符*d)
{
DIR*dirptr;
如果(访问(d,F_正常)!=-1){
//文件存在
如果((dirptr=opendir(d))!=NULL){
closedir(dirptr);/*d存在并且是一个目录*/
}否则{
return-2;/*d存在但不是目录*/
}
}否则{
返回-1;/*d不存在*/
}
返回1;
}

这是否回答了您的问题?如果文件夹不存在,那么我如何在确认它不存在后立即创建一个呢?如果您使用的是Visual Studio,
dirent.h
不可用,请参阅以获取其他选项是否确保包含的头足够,至少对于Linux?S_ISDIR仅适用于POSIX,而不是Windows,请参阅尼斯一个,但我会将
int xis_dir(char*d)
更改为
int xis_dir(const char*d)
,因为d没有被修改。我想知道
S_IFDIR
vs
S_ISDIR
并在
stat
手册页上找到了这一点:POSIX.1-1990没有描述S_IFMT、S_IFSOCK、S_IFNK、S_IFREG、S_IFBLK、S_IFDIR、S_IFCHR、S_IFFO、S_ISVTX常量,而是要求使用宏S_ISDIR()等。S_IF*常量出现在POSIX.1-2001及更高版本中