Create Directory in C++

  1. Use the std::filesystem::create_directory Function to Create a Directory in C++
  2. Use the std::filesystem::create_directories Function to Create a Directory in C++

This article will explain several methods of how to create a directory in C++.

Use the std::filesystem::create_directory Function to Create a Directory in C++

Since the C++17 version, the standard library provides the filesystem manipulation interface originally implemented as part of the Boost library. Note that all filesystem facilities are provided under the std::filesystem namespace, which we are aliasing as fs in the following examples. create_directory function is used to create a new directory in the current working directory. The function takes one mandatory argument of type std::filesystem::path, which can be also be passed as a string object, and the proper initialization is done automatically.

#include <iostream>
#include <filesystem>

using std::cout; using std::endl;
using std::system; using std::string;
namespace fs = std::filesystem;

int main() {
    string directory_name("tmp_dir");

    fs::create_directory(directory_name)?
    cout << "created directory - " << directory_name << endl :
    cout << "create_directory() failed" << endl;

    return EXIT_SUCCESS;
}

The successful function call can be checked by the return value of the create_directory, which should evaluate as true. Note that the created directory can be removed with std::filesystem::remove call, which takes the same directory path. create_directory optionally can take two-path arguments, the second of which must be an existing directory and a newly created directory will copy the attributes of the former one.

#include <iostream>
#include <filesystem>

using std::cout; using std::endl;
using std::system; using std::string;
namespace fs = std::filesystem;

int main() {
    string directory_name("tmp_dir");

    fs::create_directory(directory_name)?
    cout << "created directory - " << directory_name << endl :
    cout << "create_directory() failed" << endl;

    fs::remove(directory_name);

    return EXIT_SUCCESS;
}

Use the std::filesystem::create_directories Function to Create a Directory in C++

Another useful function is std::filesystem::create_directories, which can create multiple nested directories all specified with a single path argument. In the following example code, we demonstrate this function to create 4 level nested directories and then another one that shares the root directory with the first one. Note though, if the lasts level directory names already exist, the function returns false.

#include <iostream>
#include <filesystem>

using std::cout; using std::endl;
using std::system; using std::string;
namespace fs = std::filesystem;

int main() {

    string directory_tree = "x_tmp/level1/level2/level3";
    string directory_tree2 = "x_tmp/level1-2/level2-1";

    auto ret = fs::create_directories(directory_tree);
    if (ret) {
        cout << "created directory tree as follows: " << endl;
        std::system("tree x_tmp");
    } else {
        cout << "create_directories() failed" << endl;
    }

    auto ret2 = fs::create_directories(directory_tree2);
    if (ret2) {
        cout << "created directory tree as follows: " << endl;
        std::system("tree x_tmp");
    } else {
        cout << "create_directories() failed" << endl;
    }

    return EXIT_SUCCESS;
}

Similar to the create_directory, this function also returns a boolean value to denote a successful call. In case the removal is required std::filesystem::remove_all function can be invoked with the root directory name, it will delete the whole tree.

#include <iostream>
#include <filesystem>

using std::cout; using std::endl;
using std::system; using std::string;
namespace fs = std::filesystem;

int main() {

    string directory_tree = "x_tmp/level1/level2/level3";
    string directory_tree2 = "x_tmp/level1-2/level2-1";

    auto ret = fs::create_directories(directory_tree);
    if (ret) {
        cout << "created directory tree as follows: " << endl;
        std::system("tree x_tmp");
    } else {
        cout << "create_directories() failed" << endl;
    }

    auto ret2 = fs::create_directories(directory_tree2);
    if (ret2) {
        cout << "created directory tree as follows: " << endl;
        std::system("tree x_tmp");
    } else {
        cout << "create_directories() failed" << endl;
    }

    fs::remove_all(directory_tree.substr(0, directory_tree.find('/')));

    return EXIT_SUCCESS;
}
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - C++ Filesystem

  • Create a File in C++
  • Get Current Directory in C++