views:

710

answers:

1

I'm getting path to current directory with boost filesystem, then checking if the directory exists.

is_directory() is ok, but exists() fails on the same path, am I missing something?

Example code (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

EDIT:

#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

Fun part:

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
+1  A: 

The following is from the Boost source code:

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; }

As you can see, if is_directory returns true then exists must return true as well. Maybe the problem is elsewhere in your code - please post a minimal compilable example that shows the problem.

You may also want to try using the same file_status object for both calls, to see if maybe the output status was changing.

interjay
I've already posted code above
stefanB
You posted some snippets, not a compilable example.
interjay
copy paste and add main() { } around the code ...
stefanB
btw, I'm passing `path` to `exists()` not `file_status`, there is a function accepting `path` and returns `exists(status(path))` according to documentation
stefanB
...Still not a complete example (what goes inside the ifs?). Anyway, your new edit doesn't make sense: Are you saying that negating the if's condition changes the value that Boost returns? Probably this is a simple problem in your code, like putting a semicolon after the if.
interjay
+1 :) you are right ... I missed semicolor when I copy the code :), too many things going on at the same time ...
stefanB