转载:https://blog.csdn.net/lenkco/article/details/123306898
转载:https://blog.51cto.com/u_15414551/4399691
#include <io.h> #include <direct.h> #include <string> void ToUnixPath( std::string& s) { std::string::size_type pos = 0; while ((pos = s.find('\\', pos)) != std::string::npos) { s.replace(pos, 1, "/"); pos = pos + 2; } } void createMultiDir(const std::string& strPath) { std::string path = strPath; ToUnixPath(path); if (path[path.length() - 1] != '/') { path += '/'; } std::string::size_type pos = 0; std::string::size_type prevpos = 0; std::string tempStr(path); std::string strFolderPath; std::string stdFolderName; while ((pos = tempStr.find('/', pos)) != std::string::npos) { strFolderPath = tempStr.substr(0, pos); stdFolderName = tempStr.substr(prevpos, pos - prevpos); if (_access(strFolderPath.data(), 0) == 0)//目录存在 { pos = pos + 1; prevpos = pos; continue; } _mkdir(strFolderPath.data()); pos = pos + 1; prevpos = pos; } } int main() { std::string path = "F:/dir/Folder/test/123"; createMultiDir(path); getchar(); return 0; }