在LocalDir目录创建下级目录,NeedCreatePath是需要创建的目录,在子目录中有相关的二级目录则不用创建, 如在/tmp/test/ 下,创建 a/b/c 目录
void CreatePath(const std::string &LocalDir, std::string &NeedCreatePath)
{
int iPos = 0;
string Dir = LocalDir;
string CurCreateDir = "";
int Result;
while (iPos >= 0)
{
iPos = NeedCreatePath.find('/');
CurCreateDir = CurCreateDir + NeedCreatePath.substr(0, iPos);
Dir = Dir + CurCreateDir ;
if (-1 == (Result = access(Dir.c_str(), 0))) //该目录不存在
{
if (-1 == (Result = mkdir(Dir.c_str(), 0755))) //创建目录
{
cout << "Create path error!"<< endl;;
}
}
Dir = Dir + "/";
NeedCreatePath = NeedCreatePath.substr(iPos + 1, NeedCreatePath.size());
CurCreateDir = "";
}
}