【发布时间】:2019-05-09 11:07:20
【问题描述】:
我想获得一个绝对规范路径,给定一个在 windows 和 linux 中都使用的带有 boost 文件系统的相对路径。我希望它也适用于不存在的路径。
我正在使用自 boost 1.60 起可用的 weakly_canonical(path relativePath) 来做到这一点。但它的行为不像预期的那样(至少在 Windows 中)。
当传递没有父文件夹的相对路径时,即原始文件名或文件夹名称,例如“foo”,weakly_canonical 返回相同的未触及路径(在本例中为“foo”),而 absolute(path relativePath) 将当前路径放在前面到它(如我所料)(“current_dir/foo”)。
所以最后,我不得不先调用 absolute ,然后调用 weakly_canonical 以使其工作。
查看包含这两种情况的 sn-p。
#include <string>
#include <iostream>
#include <boost/filesystem.hpp>
using boost::filesystem;
path relativePath("foo");
path canonical_path = weakly_canonical(relativePath);
path abs_canonical_path = weakly_canonical(absolute(relativePath));
std::cout << "Using weakly_canonical: "
<< canonical_path.string()
<< std::endl;
std::cout << "Using weakly_canonical and absolute: "
<< abs_canonical_path.string()
<< std::endl;
例如,如果 current_path 是“C:\path\to\some\folder”,我正在获取:
- relativePath = "./foo"
Using weakly_canonical: C:\path\to\some\folder\foo
Using weakly_canonical and absolute: C:\path\to\some\folder\foo
- relativePath = "../foo"
Using weakly_canonical: C:\path\to\some\foo
Using weakly_canonical and absolute: C:\path\to\some\foo
- relativePath = "foo"
Using weakly_canonical: foo
Using weakly_canonical and absolute: C:\path\to\some\folder\foo
最后一种情况让我感到困惑,因为我希望weakly_canonical 也会预先添加当前的工作目录。
absolute 和weakly_canonical 一起使用是否正确?还是我滥用了weakly_canonical?
【问题讨论】:
标签: c++ boost-filesystem