符合相对目录和绝对目录
#include <iostream>
#include <fstream>
#include <sys/stat.h>
#ifdef _WIN32
#include <direct.h>
#define mkdir(path, mode) _mkdir(path)
#else
#include <direct.h>
#endif
/* 创建目录文件 */
int CreateDirectoryFile(const char* pathFile, int pathFileLength) {
std::string dir(pathFile, pathFileLength);
size_t pos = 0;
std::string token;
while ((pos = dir.find_first_of("/\\", pos)) != std::string::npos) {
token = dir.substr(0, pos);
if (!token.empty()) {
struct stat info;
#ifdef _WIN32
if (stat(token.c_str(), &info) != 0) {
mkdir(token.c_str(), 0777);
}
#else
if (stat(token.c_str(), &info) != 0) {
_mkdir(token.c_str());
}
#endif
}
pos++;
}
std::ifstream file1(pathFile);
if (file1.good()) {
file1.close();
return 0;
}
std::ofstream file(pathFile);
if (!file)
return 1;
file.close();
return 0;
}