Adrian
Adrian

Reputation: 20058

How can I change the current path using Boost.Filesystem

When starting the program I want to print the current path using current_path() ("C:\workspace\projects"). And then I want to be able to change the path to, lets say "c:\program files", so when i print again the current_path() I want to be printed "c:\program files". Something like this

int main()
{
   cout << current_path() << endl;  // c:\workspace\projects
   aFunctionToChangePath("c:\program files");
   cout << current_path() << endl;  // c:\program files
}

Is there a function in the library that I am missing so I can acomplish this ?

Upvotes: 17

Views: 10463

Answers (2)

Yakov Galka
Yakov Galka

Reputation: 72469

int main()
{
   cout << current_path() << '\n'; // c:\workspace\projects
   current_path("c:\\program files");
   cout << current_path() << '\n';  // c:\program files
}

Upvotes: 27

serup
serup

Reputation: 547

If you want to make a change to a different directory, then I suggest trying this example:

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;

Upvotes: 1

Related Questions