代码拉取完成,页面将自动刷新
#ifndef __MY_UTIL__
#define __MY_UTIL__
#define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING
#include<iostream>
#include<string>
#include<vector>
#include<sstream>
#include<time.h>
#include<fstream>
#include<sys/stat.h>
#include<experimental/filesystem>
namespace fs = std::experimental::filesystem;
namespace cloud
{
class FileUtil
{
private:
std::string _name;
public:
FileUtil(const std::string &name):_name(name)
{
}
std::string Name()
{
return fs::path(_name).filename().string();
}
bool Exists()
{
return fs::exists(_name);
}
size_t Size()
{
if(this->Exists() == false)
{
return 0;
}
return fs::file_size(_name);
}
time_t MTime()
{
if(this->Exists() == false)
{
return 0;
}
auto ftime = fs::last_write_time(_name);
std::time_t cftime = decltype(ftime)::clock::to_time_t(ftime);
return cftime;
}
time_t ATime()
{
if(this->Exists() == false)
{
return 0;
}
struct stat st;
stat(_name.c_str(),&st);
return st.st_atime;
}
bool Read(std::string *body)
{
if(this->Exists()==false)
{
return false;
}
std::ifstream ifs;
ifs.open(_name,std::ios::binary);
if(ifs.is_open() == false)
{
std::cout<<"read open failed"<<std::endl;
return false;
}
size_t fsize = this->Size();
body->resize(fsize);
ifs.read(&(*body)[0],fsize);
if(ifs.good()==false)
{
std::cout<<"read file failed"<<std::endl;
ifs.close();
return false;
}
ifs.close();
return true;
}
bool Write(const std::string &body)
{
std::ofstream ofs;
ofs.open(_name,std::ios::binary);
if(ofs.is_open() == false)
{
std::cout<<"write open failed"<<std::endl;
return false;
}
ofs.write(body.c_str(),body.size());
if(ofs.good()==false)
{
std::cout<<"write file failed"<<std::endl;
ofs.close();
return false;
}
ofs.close();
return true;
}
bool MCreateDirectory()
{
if(this->Exists())
{
return true;
}
fs::create_directories(_name);
return true;
}
bool ScanDirectory(std::vector<std::string> *arry)
{
if(this->Exists()==false)
{
return false;
}
for(auto &a : fs::directory_iterator(_name))
{
if(fs::is_directory(a) == true)
{
continue;
}
std::string pathname = fs::path(a).relative_path().string();
arry->push_back(pathname);
}
return true;
}
bool Remove()
{
if(this->Exists())
{
return true;
}
fs::remove_all(_name);
return true;
}
};
}
#endif
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。