'Creating folders in C++

I have recently started working in C++ and came across this situation when I have to create a directory while executing my code. The code is working fine when I have to create a single folder but it fails when I have to create another folder withing this newly created folder.

Suppose, I am in C: and want to store my file in C:/A/B/ .The following piece of code using mkdir() works fine if I have to store my file in C:/A/ but fails when I am adding another folder B.

Following is my code snippet:

#include <sys/stat.h>
#include <string>
using namespace std;

int main()
{
    string stringpath = "C:/A/B/"; 
    int status = mkdir(stringpath.c_str(),0777);

    if(status!=0)
    {
        //.....
    }
    else
    {
        //....
    }
}

Can someone help me in creating this directory where I can have any number of folders inside the parent directory? (P.S:I have added the header files sys/stat.h,iostream and string)



Solution 1:[1]

This is how you do it in C++17:

#include <filesystem>
namespace fs = std::filesystem;
fs::create_directories("./a/b/c")

Solution 2:[2]

mkdir() creates only the last component of the specified path. In your example, it will create only B. If any of the parent directories do not exist (ie, if A does not exist), the function fails with ENOENT. You need to split up the path and call mkdir() for every intermediate directory in the path, ignoring EEXIST errors as you go.

status = mkdir("C:/A/", 0777);
if ((status < 0) && (errno != EEXIST)) ...

status = mkdir("C:/A/B/", 0777);
if ((status < 0) && (errno != EEXIST)) ...

If you don't want to handle this manually, use a wrapper that handles it for you, such as Boost's create_directories() function:

bool create_directories(const path& p);
bool create_directories(const path& p, system::error_code& ec);

Effects: Establishes the postcondition by calling create_directory() for any element of p that does not exist.

Postcondition: is_directory(p)

Returns: true if a new directory was created, otherwise false.

Throws: As specified in Error reporting.

Complexity: O(n+1)where n is the number of elements of p that do not exist.

Solution 3:[3]

You can call the following:

string stringpath = "C:/A/B/";
int status = mkdir(stringpath.c_str(),0777);

If C:/A/ directory exists. If its not exists, then do the following:

string stringpath = "C:/A/";
int status = mkdir(stringpath.c_str(),0777);
stringpath = "C:/A/B/";
int status = mkdir(stringpath.c_str(),0777);

Solution 4:[4]

In C++11 you can use the experimental functios:

#include <experimental/filesystem>
...
std::stringstream bufH;
bufH << dirName << fName;
if (!std::experimental::filesystem::exists(bufH.str()))
{
 std::experimental::filesystem::create_directories(bufH.str());
}

Solution 5:[5]

Try the octal flag 7777 like this to have all the rights necessary to create this folder.

int status = mkdir(stringpath.c_str(), 7777);

Or do a chmod in the A folder like that : chmod -r 7777 *

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stepan Yakovenko
Solution 2
Solution 3 I. Ahmed
Solution 4 Moti Hamo
Solution 5 XmanJob