【发布时间】:2010-09-13 21:35:20
【问题描述】:
我正在使用 C,有时我必须处理类似的路径
- C:\随便
- C:\随便\
- C:\Whatever\Somefile
有没有办法检查给定路径是目录还是给定路径是文件?
【问题讨论】:
我正在使用 C,有时我必须处理类似的路径
有没有办法检查给定路径是目录还是给定路径是文件?
【问题讨论】:
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
}
【讨论】:
调用 GetFileAttributes,并检查 FILE_ATTRIBUTE_DIRECTORY 属性。
【讨论】:
使用 C++14/C++17,您可以使用来自filesystem library 的独立于平台的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();
}
}
在 C++14 中使用std::experimental::filesystem。
#include <experimental/filesystem> // C++14
namespace fs = std::experimental::filesystem;
section "File types" 中列出了其他已实施的检查。
【讨论】:
std::filesystem。确保使用带有选项-std=c++17 的Clang 7 或更高版本。 Minimal example at compiler explorer.
在 Win32 中,我通常使用PathIsDirectory 及其姐妹函数。这适用于 Windows 98,而 GetFileAttributes 不适用(根据 MSDN 文档。)
【讨论】:
GetFileAttributes(),而且据信它早于PathIsDirectory() 的存在。在检查 API 的最低操作系统要求时,您不能依赖 MSDN 文档,因为 MSDN 撒谎!当 MS 放弃对某个操作系统版本的支持时,他们希望从 MSDN 文档中删除对它的大部分引用,尤其是在现有 API 的最低操作系统要求方面。
在 Windows 上,您可以在 open handle 上使用 GetFileAttributes。
【讨论】:
这是使用GetFileAttributesW 函数检查路径是否为Windows 上的目录的简单方法。如果接收到的路径必须是目录或文件路径,那么如果不是目录路径,则可以假定它是文件路径。
bool IsDirectory(std::wstring path)
{
DWORD attrib = GetFileAttributes(path.c_str());
if ((attrib & FILE_ATTRIBUTE_DIRECTORY) != 0)
return true;
return false;
}
【讨论】:
如果你使用CFile,你可以试试
CFileStatus status;
if (CFile::GetStatus(fileName, status) && status.m_attribute == 0x10){
//it's directory
}
【讨论】:
在 qt 中更容易尝试 FileInfo.isDir()
【讨论】: