This post has already been read 3123 times!

Code source for directory tree in LINUX using C++

#include "iostream"
#include "string"
#include "sys/types.h"
#include "sys/stat.h"

using namespace std;

void mkdirTree(string sub, string dir)
{
if (sub.length() == 0)
return;

int i=0;

for (i; i < sub.length(); i++)
{
dir += sub[i];
if (sub[i] == '/')
break;
}

mkdir(dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);

if (i+1 < sub.length())
mkdirTree(sub.substr(i+1), dir);
}

int main()
{
string new_dir = "a/b/c";
mkdirTree(new_dir, "");
return 0;
}

Leave a Reply

Post Navigation