简体   繁体   English

在 C 或 C++ 中创建目录

[英]Creating a directory In C or C++

How to create a directory with C code (other than the method of forking and using mkdir) ?如何用C代码创建目录(除了fork和使用mkdir的方法)? Is there anything like dirent.h?有类似dirent.h的东西吗? dirent.h only allows to read directories. dirent.h 只允许读取目录。 (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.如果您可以使用 C++(如所选标签所建议的)和 boost 库,Boost 文件系统可以帮助您使用create_directory函数。

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.如果您不想在您的项目中使用所有 boost 库,您可以下载一个名为bcp的工具来仅提取您需要的子集,在您的情况下是 boost 文件系统及其依赖项。

Use the mkdir function .使用mkdir 函数

#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.随着c++17的出现,我们有了一个文件系统库,提供操作文件和目录的工具。 To create a directory, you can use std::filesystem::create_directory .要创建目录,您可以使用std::filesystem::create_directory

Example:例子:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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