Skip to content

Instantly share code, notes, and snippets.

@lix19937
Created August 7, 2025 08:22
Show Gist options
  • Select an option

  • Save lix19937/920a6990bd04708a9630d49cdad3a917 to your computer and use it in GitHub Desktop.

Select an option

Save lix19937/920a6990bd04708a9630d49cdad3a917 to your computer and use it in GitHub Desktop.
check_parent_exists.cc
#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