【发布时间】:2017-02-04 13:52:57
【问题描述】:
我的基本问题是这段代码几乎总是抛出异常:
bool DirectoryRange::isDirectory() const
{
struct stat s;
stat(ep->d_name, &s);
#if defined(__linux__)
if((S_ISDIR(s.st_mode) != 0) != (ep->d_type == DT_DIR))
{
throw std::logic_error("Directory is not directory");
}
#endif
return S_ISDIR(s.st_mode);
}
bool DirectoryRange::isFile() const
{
struct stat s;
stat(ep->d_name, &s);
#if defined(__linux__)
if((S_ISREG(s.st_mode) != 0) != (ep->d_type == DT_REG))
{
throw std::logic_error("File is not file");
}
#endif
return S_ISREG(s.st_mode);
}
检查 dirent 值是不可移植的,但得到了正确的答案;虽然 stat 是错误的,但它是可移植的。
那么,如果 stat 似乎不起作用,我如何可移植地检查目录?
【问题讨论】:
-
试试 Boost.Filesystem。
-
这里有点迂腐的琐事,但并非所有文件系统都有文件夹的概念。特别是大型机没有。希望您永远不需要知道这一点,但以防万一……好吧,就是这样。