为什么不按默认构造路径划分路径只是在 Visual Studio 中添加尾随分隔符?
Why Doesn't Dividing a path by the Default Constructed path Just add a Trailing Separator in Visual Studio?
假设filesystem::current_path
将返回路径:
tmp/1567519419.773012
但我想要一个尾随分隔符。如果看起来我应该做的是:
filesystem::current_path() / filesystem::path()
这适用于 gcc 给我:
/tmp/1567519419.773012/
但是在可视化工作室上,虽然filesystem::current_path
也为我提供了没有尾随分隔符的绝对路径,但除以filesystem::path()
没有任何效果。生成的路径仍然是没有尾随分隔符的绝对路径。
我想要跨平台兼容的代码,并且我想避免检测当前路径是否已经具有尾随分隔符。
有什么可供我使用的吗?
我对filesystem
不够熟悉,无法知道哪个编译器是正确的(如果涉及实现定义的行为,可能两者都是正确的(。但是,以下内容应该适用于正确实现filesystem
的所有平台:
#include <iostream>
#include <filesystem>
int main() {
auto foo = std::filesystem::current_path();
foo += foo.preferred_separator; // A static data member of std::filesystem::path
// The lexically normal form eliminates doubled separators
std::cout << foo.lexically_normal().string() << 'n';
}
相关文章:
- 如何将更多文件夹添加到c++include路径
- 对于MacOS上的G++,如何添加默认的include目录/usr/local/include和默认的库搜索路径/usr
- 如何在内核中添加包含库的路径?
- 添加 #include 的搜索路径
- 如何永久地向 g++ 编译器添加路径?
- 在 Android Studio 中添加 C/C++ 的包含路径
- 如何在Qt pro文件中添加导出路径
- 为什么不按默认构造路径划分路径只是在 Visual Studio 中添加尾随分隔符?
- "-I" 和 "-L" 在添加到路径前面时是什么意思?
- 添加一个节点,并在通用树中的两个给定节点之间找到路径成本,其中c 中的儿童列表
- 如何在忍者上添加包含路径,并为VS 2019添加叮当
- 使用连接器 c++ 访问 MYSQL 数据库(如何将路径添加到我的标准搜索目录)
- 如何将我想在C 中使用的C库的库路径添加到环境中
- Ubuntu 16.04:无法将路径添加到ld_library_path
- 是否可以强制提升构建将子项目的包含路径添加到主项目中?
- c++ 函数向文件路径添加额外的"\"?
- 训练sapi:创建转录的wav文件并将文件路径添加到注册表
- 是否有方法将相对库路径添加到可执行文件以避免设置LD_library_path
- 如何将带有空格作为参数的路径添加到CreateProcess批处理文件中
- 将包含路径添加到晶圆配置(C++)