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

How can I change the current path using Boost.Filesystem

本文关键字:当前路径 Filesystem 何使用 Boost      更新时间:2023-10-16

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

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

是否有一个函数在库中,我错过了,所以我可以完成这一点?

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

如果您想更改到另一个目录,那么我建议您尝试一下例子:

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;