Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ how to create a directory from a path

Tags:

c++

directory

what is a convenient way to create a directory when a path like this is given: "\server\foo\bar\"

note that the intermediate directories may not exist.

CreateDirectory and mkdir only seem to create the last part of a directory and give an error otherwise.

the platform is windows, MSVC compiler.

thanks!

like image 749
clamp Avatar asked Sep 03 '09 14:09

clamp


People also ask

How do you create a directory in C?

Problem: Write a C/C++ program to create a folder in a specific directory path. This task can be accomplished by using the mkdir() function. Directories are created with this function. (There is also a shell command mkdir which does the same thing).

How does mkdir work in C?

The mkdir function creates a new, empty directory with name filename . The argument mode specifies the file permissions for the new directory file. See The Mode Bits for Access Permission, for more information about this. Write permission is denied for the parent directory in which the new directory is to be added.

How do I create a directory with mkdir?

You can create directories one by one with mkdir, but this can be time-consuming. To avoid that, you can run a single mkdir command to create multiple directories at once. To do so, use the curly brackets {} with mkdir and state the directory names, separated by a comma.


1 Answers

If you can use an external library, I'd look at boost::filesystem

#include <boost/filesystem.hpp>
namespace fs=boost::filesystem;

int main(int argc, char** argv)
{
    fs::create_directories("/some/path");
}
like image 51
gnud Avatar answered Oct 23 '22 14:10

gnud