gpt4 book ai didi

c++ - 为什么不按默认构建的路径划分路径只需在 Visual Studio 中添加尾随分隔符?

转载 作者:太空狗 更新时间:2023-10-29 21:09:39 25 4
gpt4 key购买 nike

假设 filesystem::current_path 将返回路径:

/tmp/1567519419.773012

但我想要一个尾随分隔符。如果看起来我应该做的就是:

filesystem::current_path() / filesystem::path()

This works on 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++ - 为什么不按默认构建的路径划分路径只需在 Visual Studio 中添加尾随分隔符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57773492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com