在 C++ 中创建目录
-
使用
std::filesystem::create_directory
函数在 C++ 中创建目录 -
使用
std::filesystem::create_directories
函数在 C++ 中创建目录
本文将介绍几种如何在 C++ 中创建目录的方法。
使用 std::filesystem::create_directory
函数在 C++ 中创建目录
从 C++ 17 版本开始,标准库提供了最初作为 Boost 库的一部分实现的文件系统操作接口。请注意,所有文件系统功能都在 std::filesystem
命名空间下提供,在以下示例中,我们将其别名为 fs
。create_directory
函数用于在当前工作目录中创建一个新目录。该函数采用一个类型为 std::filesystem::path
的强制性参数,该参数也可以作为字符串对象传递,并且适当的初始化会自动完成。
#include <iostream>
#include <filesystem>
using std::cout; using std::endl;
using std::system; using std::string;
namespace fs = std::filesystem;
int main() {
string directory_name("tmp_dir");
fs::create_directory(directory_name)?
cout << "created directory - " << directory_name << endl :
cout << "create_directory() failed" << endl;
return EXIT_SUCCESS;
}
可以通过 create_directory
的返回值检查函数是否成功,该返回值的计算结果应为 true
。请注意,可以通过 std::filesystem::remove
调用删除创建的目录,该调用使用相同的目录路径。create_directory
可以选择使用两个路径参数,其中第二个必须是现有目录,而新创建的目录将复制前一个目录的属性。
#include <iostream>
#include <filesystem>
using std::cout; using std::endl;
using std::system; using std::string;
namespace fs = std::filesystem;
int main() {
string directory_name("tmp_dir");
fs::create_directory(directory_name)?
cout << "created directory - " << directory_name << endl :
cout << "create_directory() failed" << endl;
fs::remove(directory_name);
return EXIT_SUCCESS;
}
使用 std::filesystem::create_directories
函数在 C++ 中创建目录
另一个有用的函数是 std::filesystem::create_directories
,它可以创建多个嵌套目录,所有目录都使用单个路径参数指定。在下面的示例代码中,我们演示了此函数以创建 4 个级别的嵌套目录,然后创建另一个与第一个共享根目录的目录。但是请注意,如果最后一级目录名称已经存在,则该函数将返回 false
。
#include <iostream>
#include <filesystem>
using std::cout; using std::endl;
using std::system; using std::string;
namespace fs = std::filesystem;
int main() {
string directory_tree = "x_tmp/level1/level2/level3";
string directory_tree2 = "x_tmp/level1-2/level2-1";
auto ret = fs::create_directories(directory_tree);
if (ret) {
cout << "created directory tree as follows: " << endl;
std::system("tree x_tmp");
} else {
cout << "create_directories() failed" << endl;
}
auto ret2 = fs::create_directories(directory_tree2);
if (ret2) {
cout << "created directory tree as follows: " << endl;
std::system("tree x_tmp");
} else {
cout << "create_directories() failed" << endl;
}
return EXIT_SUCCESS;
}
与 create_directory
相似,此函数还返回一个布尔值以表示成功调用。如果需要删除,可以用根目录名称调用 std::filesystem::remove_all
函数,它将删除整个树。
#include <iostream>
#include <filesystem>
using std::cout; using std::endl;
using std::system; using std::string;
namespace fs = std::filesystem;
int main() {
string directory_tree = "x_tmp/level1/level2/level3";
string directory_tree2 = "x_tmp/level1-2/level2-1";
auto ret = fs::create_directories(directory_tree);
if (ret) {
cout << "created directory tree as follows: " << endl;
std::system("tree x_tmp");
} else {
cout << "create_directories() failed" << endl;
}
auto ret2 = fs::create_directories(directory_tree2);
if (ret2) {
cout << "created directory tree as follows: " << endl;
std::system("tree x_tmp");
} else {
cout << "create_directories() failed" << endl;
}
fs::remove_all(directory_tree.substr(0, directory_tree.find('/')));
return EXIT_SUCCESS;
}
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn