C++ 創建深層次目錄

// 將單字節轉化成寬字節的字符串
std::wstring s2ws(const std::string& s)
{
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
}
//創建深層次的路徑
BOOL CreateDeepDirectory(const char * lpPathName,        //directory name
LPSECURITY_ATTRIBUTES lpSecurityAttributes  // SD
)
{
string strPath = "";
char pszSrc[MAX_PATH] = { 0 };
strcpy(pszSrc, lpPathName);
char *ptoken = strtok(pszSrc, "\\");
while (ptoken)
{
strPath += ptoken;
strPath += "\\";
if (!access(strPath.c_str(), 0) == 0)
{   //創建失敗時還應刪除已創建的上層目錄,此次略
wstring str = s2ws(strPath);
if (!CreateDirectory(str.c_str(), lpSecurityAttributes))
{
return FALSE;
}
}
ptoken = strtok(NULL, "\\");
}
return TRUE;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章