一、mkdir模块
#include <iostream>
#include <string>
#include <sys/stat.h> // 对于 UNIX 或 Linux 系统
if (mkdir(new_file_path.c_str(), 0755) == 0)
{
std::cout << "Directory created successfully." << std::endl;
}
else
{
std::cerr << "Failed to create directory." << std::endl;
throw std::runtime_error("Failed to create directory.");
}
已经有文件的,再创建不会报错,只不过返回值不是0
二、filesystem模块
1、安装环境
sudo apt-get update
sudo apt-get install libboost-all-dev
2、递归创建目录文章来源:https://www.toymoban.com/news/detail-716516.html
#include <filesystem>
namespace fs = std::filesystem;
int main() {
fs::path dir = "/path/to/your/directory";
if (fs::create_directories(dir)) {
std::cout << "Successfully created directories!\n";
} else {
std::cout << "Failed to create directories or directories already exist!\n";
}
return 0;
}
编译文章来源地址https://www.toymoban.com/news/detail-716516.html
g++ test.cpp -lboost_filesystem -lboost_system
到了这里,关于C++ 创建文件夹的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!