如何使用Boost.Filesystem更改当前路径

时间:2011-04-15 13:41:53

标签: c++ boost

启动程序时,我想使用current_path()(“C:\ workspace \ projects”)打印当前路径。然后我希望能够改变路径,让我们说“c:\ program files”,所以当我再次打印current_path()时,我想要打印“c:\ program files”。像这样的东西

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

库中是否有我失踪的功能,所以我可以实现这个功能吗?

2 个答案:

答案 0 :(得分:20)

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

答案 1 :(得分:1)

如果您想对其他目录进行更改,那么我建议您尝试这样做 例如:

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;