boost::filesystem relative path and current directory?

57,916

Solution 1

getcwd = boost::filesystem::path full_path(boost::filesystem::current_path());

Example:

boost::filesystem::path full_path(boost::filesystem::current_path());
std::cout << "Current path is : " << full_path << std::endl;

To access current_path one needs to add #include <boost/filesystem.hpp>.

Solution 2

Try the system_complete function.

namespace fs = boost::filesystem;

fs::path full_path = fs::system_complete("../asset/toolbox");

This mimics exactly how the OS itself would resolve relative paths.

Solution 3

If you want to change to previous directory then try something like this:

boost::filesystem::path full_path( boost::filesystem::current_path() );
std::cout << "Current path is : " << full_path << std::endl;

//system("cd ../"); // change to previous dir -- this is NOT working
chdir("../"); // change to previous dir -- this IS working

boost::filesystem::path new_full_path( boost::filesystem::current_path() );
std::cout << "Current path is : " << new_full_path << std::endl;
Share:
57,916
Jake
Author by

Jake

Updated on November 26, 2020

Comments

  • Jake
    Jake over 3 years

    How can I use boost::filesystem::path to specify a relative path on Windows? This attempt fails:

    boost:filesystem::path full_path("../asset/toolbox"); // invalid path or directory.
    

    Maybe to help me debug, how to get the current working directory with boost::filesystem?