建立多級目錄

來源:互聯網
上載者:User

系統的API函數——CreateDirectory只能建立一層目錄,下面提供建立多級目錄的方法:

bool createMultipleDirectory(const char* pszDir)
{
    std::string strDir(pszDir);//存放要建立的目錄字元串
    std::vector<std::string> vPath;//存放每一層目錄字元串
    std::string strTemp;//一個臨時變數,存放目錄字元串
    bool bSuccess = false;//成功標誌

    std::string::const_iterator sIter;//定義字串迭代器
        //遍曆要建立的字串
    for (sIter = strDir.begin(); sIter != strDir.end(); sIter++) {
        if (*sIter != '//') {//如果當前字元不是'//'
            strTemp += (*sIter);
        } else {//如果當前字元是'//'
            vPath.push_back(strTemp);//將當前層的字串添加到數組中
            strTemp += '//';
        }
    }

    //遍曆存放目錄的數組,建立每層目錄
    std::vector<std::string>::const_iterator vIter;
    for (vIter = vPath.begin(); vIter != vPath.end(); vIter++) {
        //如果CreateDirectory執行成功,返回true,否則返回false
        bSuccess = CreateDirectory(vIter->c_str(), NULL) ? true : false;   
    }

    return bSuccess;
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.