#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main() {
// 指定要创建的文件夹路径
std::string folderPath = "example_folder";
try {
// 创建文件夹
if (fs::create_directory(folderPath)) {
std::cout << "文件夹创建成功: " << folderPath << std::endl;
} else {
std::cout << "文件夹已存在或创建失败: " << folderPath << std::endl;
}
} catch (const fs::filesystem_error& e) {
std::cerr << "文件系统错误: " << e.what() << std::endl;
} catch (const std::exception& e) {
std::cerr << "其他错误: " << e.what() << std::endl;
}
return 0;
}
#include <filesystem> 是 C++17 引入的标准库,提供了文件和目录操作的功能。namespace fs = std::filesystem; 为了简化代码中的调用。std::string folderPath = "example_folder"; 定义要创建的文件夹名称。fs::create_directory(folderPath) 尝试创建指定路径的文件夹。如果文件夹已经存在或创建失败,返回 false。try-catch 块捕获可能的文件系统错误和其他异常,并输出相应的错误信息。希望这段代码能帮助你理解如何在 C++ 中创建文件夹。
上一篇:c++代码在线运行
下一篇:c++数组赋值
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站