在网上找了好久都没找到能不引用一些乱七八糟的库而创建多层目录的C++代码,于是走上了自己造轮子的不归路

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <sys/stat.h>

std::string filepath = "/sdcard/xxx/xxx/xxx/xxx.json";
std::fstream file;
std::string tempFilename = "/";
char filenames[100];
std::strcpy(filenames,filepath.c_str());
char *token = std::strtok(filenames, "/");
while (token != NULL) {
tempFilename += token;
token = std::strtok(NULL, "/");
file.open(tempFilename, std::ios::in);
if (!file.is_open()){
if (tempFilename == filepath){
std::ofstream outfile;
SE_LOG_INFO(kSettingModule,"Write File");
outfile.open(filepath.c_str());
if (outfile.fail()) {
outfile.close();
SE_LOG_ERROR(kSettingModule, "Mkdir ERROR");
return (-1);
}
outfile.close();
}
else{
int flag = mkdir(tempFilename.c_str(), 0777);
if (flag != 0){
SE_LOG_ERROR(kSettingModule, "Mkdir ERROR");
return (-1);
}
}
}
file.close();
tempFilename += "/";
}