三種C/C++建立資料夾的方法
阿新 • • 發佈:2019-01-05
第一種:
呼叫MFC封裝好的介面函式,主要會用到
PathIsDirectory //判斷是否存在
::CreateDirectory //建立
例如:
#include "shlwapi.h"
#pragma comment(lib,"shlwapi.lib")
#include <afx.h>
CString path = "../../../STL/stl2";
if (!PathIsDirectory(path))
{
::CreateDirectory(path, 0);
}
第二種:
編寫C/C++函式實現該功能
例如:
#include <io.h> #include <direct.h> #define PATH_DELIMITER '\\'
bool createDirectory(const std::string folder) { std::string folder_builder; std::string sub; sub.reserve(folder.size()); for (auto it = folder.begin(); it != folder.end(); ++it) { //cout << *(folder.end()-1) << endl; const char c = *it; sub.push_back(c); if (c == PATH_DELIMITER || it == folder.end() - 1) { folder_builder.append(sub); if (0 != ::_access(folder_builder.c_str(), 0)) { // this folder not exist if (0 != ::_mkdir(folder_builder.c_str())) { // create failed return false; } } sub.clear(); } } return true; }
const std::string path2 = "..\\..\\..\\STL\\stl2";
createDirectory(path2);
第三種:
呼叫DOS命令
例如:
#include <stdlib.h>
system("md stl2");
參考:https://github.com/liuruoze/EasyPR.git