tags:

views:

146

answers:

3

How to create a directory with C code (other than the method of forking and using mkdir) ? Is there anything like dirent.h? dirent.h only allows to read directories. (without using external library)

+4  A: 

Use the mkdir function.

#include <sys/stat.h>
#include <sys/types.h>
int mkdir(const char *pathname, mode_t mode);
Douglas Leeder
Please explain mode parameter
avd
@lex: Why don't you just try and do some research for yourself?
dreamlax
@lex please read the linked man page.
Douglas Leeder
+2  A: 

Call mkdir(2).

ndim
+4  A: 

If you can use C++ (as suggested by the selected tags) and boost libraries, Boost filesystem can help you with the create_directory function.

If you don't want to have all boost libraries available in your project, you may download a tool called bcp to extract only the subset you need, in your case boost filesystem and its dependencies.

Benoît
Is there any advantage of using boost over mkdir function?
avd
Boost is pure c++, which means you don't have to convert strings to char*. Boost filesystem lets you use "path" objects which are simple to use if you work with many related directories. There are more reasons to use it, but i haven't dug into it much.
Benoît
Boost is also cross platform.
Peter Alexander