C语言 检查目录是否存在的便携式方法 [Windows/Linux, C]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18100097/
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-09-02 07:05:38  来源:igfitidea点击:

Portable way to check if directory exists [Windows/Linux, C]

cfiledirectoryfile-exists

提问by ivy

I would like to check if a given directory exists. I know how to do this on Windows:

我想检查给定的目录是否存在。我知道如何在 Windows 上执行此操作:

BOOL DirectoryExists(LPCTSTR szPath)
{
  DWORD dwAttrib = GetFileAttributes(szPath);

  return (dwAttrib != INVALID_FILE_ATTRIBUTES && 
         (dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}

and Linux:

和 Linux:

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

But I need a portable way of doing this .. Is there any way to check if a directory exists no matter what OS Im using? Maybe C standard library way?

但是我需要一种可移植的方式来做到这一点..有没有办法检查目录是否存在,无论我使用什么操作系统?也许C标准库方式?

I know that I can use preprocessors directives and call those functions on different OSes but thats not the solution Im asking for.

我知道我可以使用预处理器指令并在不同的操作系统上调用这些函数,但这不是我要求的解决方案。

I END UP WITH THIS, AT LEAST FOR NOW:

我最终得到了这个,至少现在是:

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

int dirExists(const char *path)
{
    struct stat info;

    if(stat( path, &info ) != 0)
        return 0;
    else if(info.st_mode & S_IFDIR)
        return 1;
    else
        return 0;
}

int main(int argc, char **argv)
{
    const char *path = "./TEST/";
    printf("%d\n", dirExists(path));
    return 0;
}

回答by Ingo Leonhardt

stat()works on Linux., UNIX and Windows as well:

stat() 也适用于 Linux、UNIX 和 Windows:

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

struct stat info;

if( stat( pathname, &info ) != 0 )
    printf( "cannot access %s\n", pathname );
else if( info.st_mode & S_IFDIR )  // S_ISDIR() doesn't exist on my windows 
    printf( "%s is a directory\n", pathname );
else
    printf( "%s is no directory\n", pathname );

回答by Tony The Lion

Use boost::filesystem, that will give you a portable way of doing those kinds of things and abstract away all ugly details for you.

使用boost::filesystem,这将为您提供一种可移植的方式来做这些事情并为您抽象出所有丑陋的细节。

回答by Adam Parsons

Since I found that the above approved answer lacks some clarity and the op provides an incorrect solution that he/she will use. I therefore hope that the below example will help others. The solution is more or less portable as well.

由于我发现上述批准的答案缺乏一些清晰度,并且操作提供了他/她将使用的不正确的解决方案。因此,我希望下面的示例能对其他人有所帮助。该解决方案也或多或少具有便携性。

/******************************************************************************
 * Checks to see if a directory exists. Note: This method only checks the
 * existence of the full path AND if path leaf is a dir.
 *
 * @return  >0 if dir exists AND is a dir,
 *           0 if dir does not exist OR exists but not a dir,
 *          <0 if an error occurred (errno is also set)
 *****************************************************************************/
int dirExists(const char* const path)
{
    struct stat info;

    int statRC = stat( path, &info );
    if( statRC != 0 )
    {
        if (errno == ENOENT)  { return 0; } // something along the path does not exist
        if (errno == ENOTDIR) { return 0; } // something in path prefix is not a dir
        return -1;
    }

    return ( info.st_mode & S_IFDIR ) ? 1 : 0;
}

回答by Mali

You can use the GTK glibto abstract from OS stuff.

您可以使用GTK glib从操作系统中抽象出来。

glib provides a g_dir_open()function which should do the trick.

glib 提供了一个g_dir_open()函数,它应该可以解决问题。