提高::文件系统的存在()上的目录路径发生故障,但is_directory()是确定

StackOverflow https://stackoverflow.com/questions/2203919

  •  18-09-2019
  •  | 
  •  

我越来越路径当前目录提高文件系统,然后检查是否该目录存在。

is_directory()是好的,但在同一路径上exists()失败了,我失去了什么?

实施例的代码(升压1.35):

#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>

namespace fs = boost::filesystem;

// the path is full path /home/my/somedirectory    
fs::path data_dir(fs::current_path());

fs::is_directory(data_dir)  // true, directory exists

fs::exists(data_dir)   // false
exists(status(data_dir))  // false

修改

#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
namespace fs = boost::filesystem;

fs::path data_dir(fs::current_path());
// data_dir == "/home/myusername/proj/test"

if (fs::is_directory(data_dir))             // true - is directory
if (fs::is_directory(fs::status(data_dir))) // true - it is still a directory

有趣的部分:

if (fs::exists(fs::status(data_dir)))       // true - directory exists
if (fs::exists( data_dir ))                 // true - directory exists

BUT:

if (!fs::exists(fs::status(data_dir)))      // false - directory still exists
if (!fs::exists( data_dir ))                // true  - directory does not exists
有帮助吗?

解决方案

以下是从升压源代码:

inline bool is_directory( file_status f ) { return f.type() == directory_file; }
inline bool exists( file_status f )       { return f.type() != status_unknown && f.type() != file_not_found; }

你可以看到,如果is_directory返回true,则exists必须返回也是如此。也许问题出在你的代码的其他地方 - 请发表最小编译的例子,显示的问题。

您还可以尝试使用相同file_status对象两个电话,看是否可能输出status发生了变化尝试。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top