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)

Leave a Comment