获取当前路径
因为要使能windows与linux通用,所以windows下必须把“\”替换为“/”,此处使用正则表达式替换
#include <regex>
#ifdef _WIN32
#include <direct.h>
#else
#include <unistd.h>
#include <sys/stat.h>
#endif
string getCurrentPath()
{
char buf[1024] = "";
string path = string();
#ifdef _WIN32
getcwd(buf, sizeof(buf));
regex reg("\\\\");
path = regex_replace(buf, reg, "/");
#else
getcwd(buf, sizeof(buf));
path = string(buf);
#endif
return path;
}
创建多级路径
int mkmuldir(const string &path)
{
#ifdef _WIN32
if(access(path.c_str(), 0) == 0)
{
return 0;
}
int index = path.find("/", 0);
while(true)
{
string tPath = path.substr(0, index);
if(access(tPath.c_str(),0) == -1)
{
if( mkdir(tPath.c_str()) < 0)
return -1;
}
index = path.find("/", index + 1);
if(index < 0)
{
break;
}
}
#else
if(access(path.c_str(), 0) == 0)
{
return 0;
}
int index = path.find("/", 1);
while(true)
{
string tPath = path.substr(0, index);
if(access(tPath.c_str(),0) == -1)
{
if(mkdir(tPath.c_str(), 755) < 0)
return -1;
}
index = path.find("/", index + 1);
if(index < 0)
{
break;
}
}
#endif
return 0;
}