I was thinking to migrate from std::experimental::filesystem
to std::filesystem
, but my unit test started to break. I use Windows, VS2019 and MSVC compiler with /std:c++latest
.
I narrowed the issue down to the difference between append methods (and operator/=
) from standard and experimental code bases.
#include <filesystem>
#include <experimental/filesystem>
int main() {
std::filesystem::path p1 = "C:\\temp";
p1 /= "\\test\\file.txt"; // Expected: C:\temp\test\file.txt, Actual C:\test\file.txt
std::experimental::filesystem::path p2 = "C:\\temp";
p2 /= "\\test\\file.txt"; // Expected: C:\temp\test\file.txt, Actual C:\temp\test\file.txt
}
Question : how can I use std::filesystem
to append two path components on Windows to get C:\\temp\\test\\file.txt
?
当您附加绝对路径时,它会替换p1
所有组件,您需要从\\\\test\\\\file.txt
删除前导斜杠以使其成为相对路径,以便将其附加到p1
:
p1 /= "test\\file.txt";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.