boost::filesystem::path::lexically_normal()
的文档指出:
Returns
*this
with redundant current directory (dot), parent directory (dot-dot), and directory-separator elements removed.
参见:http://www.boost.org/doc/libs/1_63_0/libs/filesystem/doc/reference.html .
以下打印 ./test
(使用 Boost 1.63.0),我希望 test
:
#include <boost/filesystem/path.hpp>
#include <iostream>
int main(void)
{
std::cout << boost::filesystem::path{"./test"}.lexically_normal().string() << "\n";
return 0;
}
所以第一个点元素不被认为是多余的。但是,test
和 ./test
在使用 boost 文件系统时显然解析为同一个文件,所以这对我来说似乎不一致。这是预期的行为吗?
更新:C++17 文件系统库按预期返回“test”
(GCC 8.2.0,Linux)。
最佳答案
这是合理的行为,因为尽管您认为 test
和 ./test
通常指的是同一事物,但并非所有地方都是这种情况。
例如,如果您在 shell 中将 ./test
作为命令运行,它将始终在当前目录中查找该程序,而不会在其他任何地方查找。但是,如果您运行 test
,它将查找运行时路径(例如 $PATH
)。
所以 test
和 ./test
是否引用同一个文件实际上是上下文相关的——因此 ./
不是多余的。
关于c++ - boost::filesystem::path::lexically_normal: 这是不正确的行为吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42952605/