来源:https://stackoverflow.com/questions/71658440/c17-create-directories-automatically-given-a-file-path
#include <string>
#include <filesystem>
// Returns:
// true upon success.
// false upon failure, and set the std::error_code & err accordingly.
bool CreateDirectoryRecursive(std::string const & dirName, std::error_code & err)
{
err.clear();
if (!std::filesystem::create_directories(dirName, err))
{
if (std::filesystem::exists(dirName))
{
// The folder already exists:
err.clear();
return true;
}
return false;
}
return true;
}