目录中的分隔符为‘/’,当存在重复的'//'时候会自动合并一个。
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);
}
本文详细解析了C++中用于创建目录的函数`mkdirs`,包括其核心逻辑、工作原理以及如何处理路径中的重复斜杠。通过实践示例,深入理解该函数在文件系统操作中的应用。
2049

被折叠的 条评论
为什么被折叠?



