Reputation: 20058
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
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
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