[英]Return value of std::filesystem::create_directories() on paths with trailing slash
[英]std::filesystem::create_directories Visual Studio 2017
我正在使用 Visual Studio 社区 2017 15.8.2。 我正在尝试使用此 SO 线程中的解决方案为路径的每个元素创建一个目录(如果它不存在)。
但是,在调用create_directories
线上我收到一个错误: namespace "std::experimental::filesystem" has no member "create_directories."
我尝试了std::experimental::filesystem
和std::filesystem
没有成功。
我尝试通过将语言标准设置为ISO C++17 Standard (/std:c++17)
来将项目属性中的 c++ 版本更改为 c++17,但仍然是相同的错误。
当我进入文件系统命名空间查看标题时,我发现没有 create_directories 函数。
根据this应该有这样一个函数,但是在我正在阅读的头文件中,我只能看到所有这些类和函数中的path
类。 头文件的位置是: C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Tools\\MSVC\\14.15.26726\\include
这是我的代码的样子:
#include "pch.h"
#include <fstream>
int main(){
namespace fs = std::experimental::filesystem; // In C++17 use std::filesystem.
try {
fs::create_directories("C:\\Program Files\\Test");
}
catch (std::exception& e) { // Not using fs::filesystem_error since std::bad_alloc can throw too.
std::cout << e.what() << std::endl;
}
}
回答我自己的问题。 问题是我忘记包含实验/文件系统标头。 现在一切正常。 这是一个愚蠢的错误,但我是 C++ 的新手。
因此,解决方案是在我的代码中添加: #include <experimental/filesystem>
。
这是我的代码现在的样子:
#include "pch.h"
#include <fstream>
#include <experimental/filesystem>
int main(){
namespace fs = std::experimental::filesystem; // In C++17 use std::filesystem.
try {
fs::create_directories("C:\\Program Files\\Test");
}
catch (std::exception& e) { // Not using fs::filesystem_error since std::bad_alloc can throw too.
std::cout << e.what() << std::endl;
}
}
当我以为我正在查看文件系统头文件时,我实际上正在查看 fstream 头文件。 这就是为什么我找不到我要找的功能的原因。 欺骗我的是 fstream 标头也有一个名为filesystem
的命名空间,所以我实际上正在查看它(因为 Visual Studio 将我指向该标头,因为它是我包含的唯一具有文件系统命名空间的标头。
此外,由于我将语言设置更改为 ISO C++17 标准,因此我也可以使用#include <filesystem>
和std::filesystem
代替。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.