如何判断给定路径是目录还是文件?(C/C++)

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

How can I tell if a given path is a directory or a file? (C/C++)

c++cwinapi

提问by DylanJ

I'm using C and sometimes I have to handle paths like

我正在使用 C,有时我必须处理类似的路径

  • C:\Whatever
  • C:\Whatever\
  • C:\Whatever\Somefile
  • C:\随便
  • C:\随便\
  • C:\随便\某个文件

Is there a way to check if a given path is a directory or a given path is a file?

有没有办法检查给定路径是目录还是给定路径是文件?

采纳答案by Colen

Call GetFileAttributes, and check for the FILE_ATTRIBUTE_DIRECTORY attribute.

调用GetFileAttributes,并检查 FILE_ATTRIBUTE_DIRECTORY 属性。

回答by

stat() will tell you this.

stat() 会告诉你这个。

struct stat s;
if( stat(path,&s) == 0 )
{
    if( s.st_mode & S_IFDIR )
    {
        //it's a directory
    }
    else if( s.st_mode & S_IFREG )
    {
        //it's a file
    }
    else
    {
        //something else
    }
}
else
{
    //error
}

回答by jeffm

In Win32, I usually use PathIsDirectoryand its sister functions. This works in Windows 98, which GetFileAttributes does not (according to the MSDN documentation.)

在 Win32 中,我通常使用PathIsDirectory及其姊妹函数。这适用于 Windows 98,而 GetFileAttributes 不适用(根据 MSDN 文档。)

回答by Roi Danton

With C++14/C++17 you can use the platform independent is_directory()and is_regular_file()from the filesystem library.

随着C ++ 14 / C ++的平台独立17可以使用is_directory()is_regular_file()文件系统库

#include <filesystem> // C++17
#include <iostream>
namespace fs = std::filesystem;

int main()
{
    const std::string pathString = "/my/path";
    const fs::path path(pathString); // Constructing the path from a string is possible.
    std::error_code ec; // For using the non-throwing overloads of functions below.
    if (fs::is_directory(path, ec))
    { 
        // Process a directory.
    }
    if (ec) // Optional handling of possible errors.
    {
        std::cerr << "Error in is_directory: " << ec.message();
    }
    if (fs::is_regular_file(path, ec))
    {
        // Process a regular file.
    }
    if (ec) // Optional handling of possible errors. Usage of the same ec object works since fs functions are calling ec.clear() if no errors occur.
    {
        std::cerr << "Error in is_regular_file: " << ec.message();
    }
}

In C++14 use std::experimental::filesystem.

在 C++14 中使用std::experimental::filesystem.

#include <experimental/filesystem> // C++14
namespace fs = std::experimental::filesystem;

Additional implemented checks are listed in section "File types".

其他实施的检查列在“文件类型”部分

回答by Scott

On Windows you can use GetFileAttributeson an open handle.

在 Windows 上,您可以在打开的句柄上使用GetFileAttributes

回答by anurag-jain

If you're using CFileyou can try

如果您正在使用,CFile您可以尝试

CFileStatus status;
    if (CFile::GetStatus(fileName, status) && status.m_attribute == 0x10){
       //it's directory
}

回答by Varvara

Easier to try FileInfo.isDir() in qt

更容易在 qt 中尝试 FileInfo.isDir()