Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

c++ - std::filesystem::create_directories Visual Studio 2017

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 Studio2017CommunityVCToolsMSVC14.15.26726include

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;
}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...