目录中的分隔符为‘/’,当存在重复的'//'时候会自动合并一个。
void mkdirs(const char *dir)
{
std::string strPath;
if (strlen(dir) == 0 || dir == nullptr)
{
printf("strlen(dir) is 0 or dir is NULL./n");
return;
}
strPath = dir;
size_t nPos = 0, nIndex = 0;
std::string strSub;
while((nPos = strPath.find('/', nIndex)) != std::string::npos)
{
strSub = strPath.substr(0, nPos);
if(!strSub.empty())
mkdir(strSub.c_str(), 0777);
nIndex = nPos + 1;
while(strPath.length() > nIndex && strPath.at(nIndex) == '/')
{
strPath.replace(nPos, 2, "/");
}
if(strPath.length() <= nIndex)
break;
}
if(strSub.compare(strPath) != 0)
mkdir(strSub.c_str(), 0777);
}