2011-04-15 46 views
9

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

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

在图书馆有一个函数,我失踪了,所以我可以实现这个吗?

+0

是否允许为操作系统的具体情况? – RedX 2011-04-15 13:44:12

+0

是的,它也是这样的 – Kobe 2011-04-15 13:50:21

回答

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

嗯,这很简单,我不知道你可以做到这一点。非常感谢 – Kobe 2011-04-15 14:13:02

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; 
+0

虽然这个链接可能回答这个问题,但最好在这里包含答案的重要部分,并提供供参考的链接。如果链接页面更改,则仅链接答案可能会失效。 – Jeroen 2014-03-05 13:45:03

+0

我同意 - 所以我纠正了它 – serup 2014-03-05 13:58:42