C++ 代码实现分层创建目录
int mkpath ( std::string s, mode_t mode)
{
size_t pos = 0;
std::string dir;
int ret = 0;
if (s[s.size()- 1] != '/') {
s += '/';
}
while( (pos = s.find_first_of('/', pos)) != std::string::npos ){
dir = s.substr( 0, pos++ );
if ( dir.size() == 0 )
continue;
if ( (ret = mkdir(dir.c_str(), mode)) && errno != EEXIST){
printf("ret=%d,pos=%lu,dir=%s\r\n",ret, pos,dir.c_str());
return ret;
}
printf("ret=%d,pos=%lu,dir=%s\r\n",ret, pos,dir.c_str());
}
return ret;
}