简体   繁体   中英

Creating a directory In C or C++

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)

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.

Use the mkdir function .

#include <sys/stat.h>
#include <sys/types.h>
int mkdir(const char *pathname, mode_t mode);

调用mkdir(2)

With the advent of c++17 we have a Filesystem library providing facilities for manipulating files and directory. To create a directory, you can use std::filesystem::create_directory .

Example:

#include <filesystem>
std::filesystem::create_directory("newdir");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM