-
-
Save lix19937/920a6990bd04708a9630d49cdad3a917 to your computer and use it in GitHub Desktop.
check_parent_exists.cc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <experimental/filesystem> | |
| namespace fs = std::experimental::filesystem; | |
| void ensure_parent_exists(const std::string& filepath) { | |
| fs::path p(filepath); | |
| fs::path parent = p.parent_path(); | |
| if (!parent.empty() && !fs::exists(parent)) { | |
| std::cout << "Creating directory: " << parent << std::endl; | |
| fs::create_directories(parent); | |
| } | |
| } | |
| void ensure_parent_exists2(const std::string& filepath) { | |
| fs::create_directories(fs::path(filepath).parent_path()); | |
| } | |
| int main() { | |
| ensure_parent_exists2("./tmp/subdir/file.txt"); | |
| return 0; | |
| } | |
| // g++ test_fs.cpp -std=c++11 -lstdc++fs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment