static bool check_exists(const std::string &file_path) {
return (access(file_path.c_str(), F_OK) != -1);
}
static std::string get_parent_path(const std::string &path) {
char buf[1024];
strcpy(buf, path.c_str());
char *result = dirname(buf);
return std::string(result);
}
//创建多级目录
static void create_directories(const std::string &path_dir) {
if (check_exists(path_dir)) {
return;
}
std::vector<std::string> path_list({path_dir});
std::string temp_dir_str = path_dir;
while (true) {
if ((temp_dir_str.find("/") != std::string::npos) &&
(temp_dir_str != "/")) {
temp_dir_str = get_parent_path(temp_dir_str);
if (!check_exists(temp_dir_str)) {
path_list.push_back(temp_dir_str);
} else {
break;
}
} else {
break;
}
}
for (auto iter = path_list.rbegin(); iter != path_list.rend(); ++iter) {
mkdir((*iter).c_str(), 0775);
}
}
03-27
586
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)