boost::filesystem 相对路径和当前目录?

发布于 2024-09-27 04:16:24 字数 237 浏览 4 评论 0原文

如何使用 boost::filesystem::path 在 Windows 上指定相对路径?这次尝试失败了:

boost:filesystem::path full_path("../asset/toolbox"); // invalid path or directory.

也许是为了帮助我调试,如何使用 boost::filesystem 获取当前工作目录?

How can I use boost::filesystem::path to specify a relative path on Windows? This attempt fails:

boost:filesystem::path full_path("../asset/toolbox"); // invalid path or directory.

Maybe to help me debug, how to get the current working directory with boost::filesystem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

赏烟花じ飞满天 2024-10-04 04:16:25

如果您想更改到上一个目录,请尝试以下操作:

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;

If you want to change to previous directory then try something like this:

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;
彩虹直至黑白 2024-10-04 04:16:25

当你输入“../your/path”时,你不是指定了一个类似unix的路径吗?我认为要获取系统特定路径,您应该做的是:

boost:filesystem::path full_path(".." / "asset" / "toolbox");

在这种情况下,“/”是以系统特定方式连接路径的运算符,而不是您指定的路径的一部分。

When you type "../your/path" aren't you specifying a unix-like path? I think what you should do to get system specific paths is:

boost:filesystem::path full_path(".." / "asset" / "toolbox");

In this case the '/' is an operator concatenating paths in a system specific way and is not part of the path that you specify.

抱着落日 2024-10-04 04:16:24
getcwd = boost::filesystem::path full_path(boost::filesystem::current_path());

示例:

boost::filesystem::path full_path(boost::filesystem::current_path());
std::cout << "Current path is : " << full_path << std::endl;

要访问 current_path,需要添加 #include

getcwd = boost::filesystem::path full_path(boost::filesystem::current_path());

Example:

boost::filesystem::path full_path(boost::filesystem::current_path());
std::cout << "Current path is : " << full_path << std::endl;

To access current_path one needs to add #include <boost/filesystem.hpp>.

等待圉鍢 2024-10-04 04:16:24

尝试 system_complete< /a> 函数。

namespace fs = boost::filesystem;

fs::path full_path = fs::system_complete("../asset/toolbox");

这完全模仿了操作系统本身如何解析相对路径。

Try the system_complete function.

namespace fs = boost::filesystem;

fs::path full_path = fs::system_complete("../asset/toolbox");

This mimics exactly how the OS itself would resolve relative paths.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文