在 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