How to Create Directory Tree in C++/Linux

How can I create directory tree in C++/Linux?

Easy with Boost.Filesystem: create_directories

#include <boost/filesystem.hpp>
//...
boost::filesystem::create_directories("/tmp/a/b/c");

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

How can I create directory tree in C?

Here is a small C program to create the directory tree a/b/c in the current directory:

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <errno.h>

int create_dir(char *name)
{
int rc;

rc = mkdir(name, S_IRWXU);
if (rc != 0 && errno != EEXIST)
{
perror("mkdir");
exit(1);
}
if (rc != 0 && errno == EEXIST)
printf("%s already exists.\n", name);

return 0;
}

int main(int argc, char **argv)
{

create_dir("a");
create_dir("a/b");
create_dir("a/b/c");

exit(0);
}

Creating a new directory in C

Look at stat for checking if the directory exists,

And mkdir, to create a directory.

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

struct stat st = {0};

if (stat("/some/directory", &st) == -1) {
mkdir("/some/directory", 0700);
}

You can see the manual of these functions with the man 2 stat and man 2 mkdir commands.

Creating a full directory tree at once

Change shebang to

#!/bin/bash

to run the script with bash as it supports brace expansion.

The problem is that you are using shell that does not support it. Your /bin/sh does not point to /bin/bash but to something like /bin/dash.

https://wiki.ubuntu.com/DashAsBinSh#A.7B

Making a directory tree in C in multi-threaded application

In libc, mkdir can set the error value EEXIST which means 'that directory already exists'. Thanks Jonathan Leffler "errno is thread-safe as long as you tell the compilation to make things thread-safe".

Creating directories is monotonic - you are always adding new ones, not removing them. So you can create a directory tree (attempting to create each directory at each level) and if some other thread got there first, it's not a problem, keep going.

If I were you, I would have each thread create its entire path recursively, ignoring errors. When it is complete building its path, it should then test whether the directory exists. If it does not exist, that is a problem (as the sequence of mkdir operations that you used to create the required path will be synchronous within the thread). If it does exist, congratulations.

Recursive mkdir() system call on Unix

There is not a system call to do it for you, unfortunately. I'm guessing that's because there isn't a way to have really well-defined semantics for what should happen in error cases. Should it leave the directories that have already been created? Delete them? What if the deletions fail? And so on...

It is pretty easy to roll your own, however, and a quick google for 'recursive mkdir' turned up a number of solutions. Here's one that was near the top:

http://nion.modprobe.de/blog/archives/357-Recursive-directory-creation.html

static void _mkdir(const char *dir) {
char tmp[256];
char *p = NULL;
size_t len;

snprintf(tmp, sizeof(tmp),"%s",dir);
len = strlen(tmp);
if (tmp[len - 1] == '/')
tmp[len - 1] = 0;
for (p = tmp + 1; *p; p++)
if (*p == '/') {
*p = 0;
mkdir(tmp, S_IRWXU);
*p = '/';
}
mkdir(tmp, S_IRWXU);
}

Simulation tree command in C

  1. You must define tree before you use it or declare a prototype.
  2. tree and main need return types.
  3. path is not defined and ruta is used. Presumably these should be the same thing.
  4. You never call stat to fill buf with the file you got in dp from readdir.

Special bonus: nivel is a bad idea. It would make better sense to have it as a parameter and have the root level pass in 0 and then, in each call to the child, pass nivel+1.

Also, “it compiles” says nothing about “it works”, especially in C.



Related Topics



Leave a reply



Submit