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

ivy*_*ivy 59 c directory file file-exists

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

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

  return (dwAttrib != INVALID_FILE_ATTRIBUTES && 
         (dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}
Run Code Online (Sandbox Code Playgroud)

和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. */
}
Run Code Online (Sandbox Code Playgroud)

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

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

我最终用这个,至少现在:

#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;
}
Run Code Online (Sandbox Code Playgroud)

Ing*_*rdt 86

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 );
Run Code Online (Sandbox Code Playgroud)

  • 在 Xubuntu 12.04 和 Windows XP 上检查了这个,它有效,谢谢! (2认同)
  • 我无法让它在 OSX 10.11 上工作。XCode7 说:“没有匹配的函数调用 stat”。修复方法是让我在 stat() 调用中执行 pathname.c_str() ,因为我的路径名是 std::string。 (2认同)

Iva*_*tin 10

使用 C++17,您可以使用std::filesystem::is_directory函数(https://en.cppreference.com/w/cpp/filesystem/is_directory)。它接受一个std::filesystem::path可以用 unicode 路径构造的对象。

  • 谢谢您,因为这是标准库的一部分,不需要臃肿的第三方库,这应该是公认的答案 (3认同)

Ada*_*ons 9

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

/******************************************************************************
 * 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;
}
Run Code Online (Sandbox Code Playgroud)

  • 我不会投反对票。但是,如果您要提供一个更清晰的示例,最好还包含必要的包含。整个堆栈溢出页面(您的答案和其他答案)上都没有提到“#include &lt;errno.h&gt;”。但是从我完成的其他阅读中,我知道这是一个缺失的导入。 (2认同)

Ton*_*ion 6

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