判断文件夹是否存在
#include <sys/stat.h>
#include <unistd.h>
#include <fstream>
#include <string>
#include <iostream>
bool isFileExists_access(string& name) {
return (access(name.c_str(), F_OK ) != -1 );
}
bool isFileExists_stat(string& name) {
struct stat buffer;
return (stat(name.c_str(), &buffer) == 0);
}
经过测试,isFileExists_stat耗时最短。
参考:
https://zhuanlan.zhihu.com/p/180501394
创建文件夹
#include <iostream>
#include <string>
#include <sys/stat.h>
#include <errno.h>
namespace light
{
int mkpath(std::string s, mode_t mode = 0755)
{
size_t pre = 0, pos;
std::string dir;
int mdret;
if (s[s.size() - 1] != '/')
{
// force trailing / so we can handle everything in loop
s += '/';
}
while ((pos = s.find_first_of('/', pre)) != std::string::npos)
{
dir = s.substr(0, pos++);
pre = pos;
if (dir.size() == 0)
continue; // if leading / first time is 0 length
if ((mdret = ::mkdir(dir.c_str(), mode)) && errno != EEXIST)
{
return mdret;
}
}
return mdret;
}
}