c++ 新建文件目录 示例
时间: 2024-06-22 21:01:40 浏览: 104
在C++中,创建新的文件或目录通常涉及到操作系统的文件系统接口。但是请注意,标准库并没有提供直接创建目录的功能,因为这通常是操作系统级别的权限管理。你可以使用`<filesystem>`库(C++20引入)来处理这种操作,如果目标系统支持的话。如果没有该库,你可能需要借助`boost filesystem`或其他第三方库。
以下是一个使用`<filesystem>`库创建新目录的示例(假设编译器支持C++20):
```cpp
#include <filesystem>
namespace fs = std::filesystem;
void createDirectoryIfNotExists(const std::string& path) {
try {
if (!fs::exists(path)) {
fs::create_directory(path);
std::cout << "Directory created: " << path << std::endl;
} else {
std::cout << "Directory already exists: " << path << std::endl;
}
} catch (const std::exception& e) {
std::cerr << "Error creating directory: " << e.what() << std::endl;
}
}
int main() {
std::string dirPath = "path/to/your/directory";
createDirectoryIfNotExists(dirPath);
阅读全文
相关推荐


















