ste*_*anB 20 boost boost-filesystem
我正在使用boost文件系统获取当前目录的路径,然后检查目录是否存在.
is_directory()exists()没问题,但在相同的路径上失败了,我错过了什么吗?
示例代码(boost 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
Run Code Online (Sandbox Code Playgroud)
编辑:
#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
Run Code Online (Sandbox Code Playgroud)
有趣的部分:
if (fs::exists(fs::status(data_dir))) // true - directory exists
if (fs::exists( data_dir )) // true - directory exists
Run Code Online (Sandbox Code Playgroud)
但:
if (!fs::exists(fs::status(data_dir))) // false - directory still exists
if (!fs::exists( data_dir )) // true - directory does not exists
Run Code Online (Sandbox Code Playgroud)
int*_*jay 20
以下是来自Boost的源代码:
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; }
Run Code Online (Sandbox Code Playgroud)
如您所见,如果is_directory返回true,则exists必须返回true.也许问题出在代码的其他地方 - 请发布一个显示问题的最小可编译示例.
您可能还想尝试file_status对两个调用使用相同的对象,以查看输出是否status正在更改.
| 归档时间: |
|
| 查看次数: |
67395 次 |
| 最近记录: |