std :: filesystem :: create_directories Visual Studio 2017 [英] std::filesystem::create_directories Visual Studio 2017
问题描述
我正在使用Visual Studio Community 2017 15.8.2.我正在尝试使用
我尝试使用 std :: experimental :: filesystem
和 std :: filesystem
都没有成功.
我尝试通过将语言标准设置为 ISO C ++ 17 Standard(/std:c ++ 17)
来将项目属性中的c ++版本更改为c ++ 17,但仍然存在相同的错误./p>
当我进入文件系统名称空间以查看标头时,我发现没有create_directories函数.
根据此,应该有这样的功能,但是在我正在阅读的头文件在所有这些类和函数中只看到 path
类.头文件的位置是:C:\ Program Files(x86)\ Microsoft Visual Studio \ 2017 \ Community \ VC \ Tools \ MSVC \ 14.15.26726 \ include
这是我的代码的样子:
#include"pch.h"#include< fstream>int main(){命名空间fs = std :: experimental :: filesystem;//在C ++ 17中使用std :: filesystem.尝试 {fs :: create_directories("C:\\ Program Files \\ Test");}catch(std :: exception& e){//不使用fs :: filesystem_error,因为std :: bad_alloc也可能抛出.std :: cout<<e.what()<<std :: endl;}}
回答我自己的问题.问题是我忘了包含实验性/文件系统标头.现在一切正常.这是一个愚蠢的错误,但是我对C ++还是陌生的.
因此,解决方案是在我的代码中添加: #include< experimental/filesystem>
.
这是我的代码现在的样子:
#include"pch.h"#include< fstream>#include< experimental/filesystem>int main(){命名空间fs = std :: experimental :: filesystem;//在C ++ 17中使用std :: filesystem.尝试 {fs :: create_directories("C:\\ Program Files \\ Test");}catch(std :: exception& e){//不使用fs :: filesystem_error,因为std :: bad_alloc也可能抛出.std :: cout<<e.what()<<std :: endl;}}
当我以为我正在查看文件系统头文件时,我才敏锐地查看了fstream头文件.这就是为什么我找不到我想要的功能的原因.困扰我的是fstream标头还具有一个名为 filesystem
的命名空间,因此我实际上是在查看它(因为Visual Studio将我指向该标头,因为它是我包括的唯一标头)其中的文件系统名称空间.
此外,由于我将语言设置更改为ISO C ++ 17 Standard,所以我也可以改用 #include< filesystem>
和 std :: filesystem
./p>
I am using Visual Studio Community 2017 15.8.2. I am trying to use the solution from this SO thread to create a directory for every element of the path if it doesn't exist.
However, on line which calls create_directories
I get an error: namespace "std::experimental::filesystem" has no member "create_directories."
I tried with both std::experimental::filesystem
and std::filesystem
with no success.
I tried changing c++ version in project properties to c++17 by setting the language standard to ISO C++17 Standard (/std:c++17)
, still same error.
When I step into filesystem namespace to see the header I see that there is no create_directories function.
According to this there should be such a function, but in the header file I'm reading I only see the path
class out of all those classes and functions. The location of the header file is:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.15.26726\include
Here is how my code looks like:
#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;
}
}
To answer my own question. The problem was that I forgot to include the experimental/filesystem header. Now everything's working perfectly. It was a silly mistake, but I am new to c++.
So, the solution was to add: #include <experimental/filesystem>
to my code.
Here is how my code looks like now:
#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;
}
}
When I thought I was looking at the filesystem header file, I was acually looking at the fstream header file instead. That's why I couldn't find the functions I was looking for. The thing that tricked me was that fstream header also has a namespace called filesystem
, so I was actually looking at that (since Visual Studio pointed me to that header, since it was the only header I included that has filesystem namespace in it.
Also, since I changed the language settings to ISO C++17 Standard I could also use #include <filesystem>
and std::filesystem
instead.
这篇关于std :: filesystem :: create_directories Visual Studio 2017的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!