目录
文件工具类主要是创建一个文件对象将磁盘上的一个文件关联起来,方便对该文件进行操作和获取文件的信息等操作,包括获取文件的内容,文件的大小,文件的访问时间,往文件中写入内容等操作。
文件的 成员变量是filename,它存储程序能够访问到文件的相对路径和绝对路径,通过filename能够对文件进行各种操作。
-
- class FileUtil{
- private:
- std::string filename;
- public:
- FileUtil(const char* str){
- filename=str;
- }
-
- FileUtil(const std::string s){
- filename=s;
- }
- size_t GetFileSize(); //获取文件大小
- size_t GetFileAccessTime() ;//获取文件最后一次访问时间
- size_t GetFileModfityTime();//获取文件最后一次修改时间
- std::string GetFilename(); //获取文件名
- bool Exist(); //判断文件是否存在
- bool SetContent(std::string& body);//往文件中写入body的内容
- bool GetContent(std::string& body);//获取文件内容,将文件内容放入body中
- //获取文件的部分内容
- bool GetPosContent(size_t pos,size_t n,std::string& body);
- bool Remove();//删除文件
- //压缩文件,压缩文件名为packname
- bool PackFile(const std::string&packname);
- //解压缩文件,解压缩文件名为packname
- bool UnpackFile(const std::string& unpackname);
- //获取目录下的所有文件的相对路径
- bool GetPathInDir(std::vector
& arry) ; - //创建目录
- bool CreateDir(std::string& dirname);
- };
stat接口可以直接判断文件是否存在,直接获取到文件的大小,文件最后一次进入时间,文件最后一次修改时间
stat接口通过文件相对路径或者绝对路径来访问到文件,获取到文件属性信息.
int stat(const char *path, struct stat *buf);
返回值: 如果通过文件路径找不到文件,则返回-1,找到了返回0.
文件的属性信息存储到struct stat对象中,struct stat结构体的成员有:
- struct stat {
- dev_t st_dev; /* 包含文件的设备IDe */
- ino_t st_ino; /* inode number */
- mode_t st_mode; /* protection */
- nlink_t st_nlink; /* 硬链接数 */
- uid_t st_uid; /* 文件所用者的ID*/
- gid_t st_gid; /* 文交所属组的ID */
- dev_t st_rdev; /* 设备ID */
- off_t st_size; /* 文件的总大小*/
- blksize_t st_blksize; /* 文件系统I/O的块大小 */
- blkcnt_t st_blocks; /* 分配的512B块数 */
- time_t st_atime; /* 最后一次进入的时间 */
- time_t st_mtime; /* 最后一次访问的时间 */
- time_t st_ctime; /* 上次状态更改时*/
- };
- size_t sjp::FileUtil::GetFileSize(){
- struct stat buf;
- if(stat(filename.c_str(),&buf)==-1){
- cout<<"GetFileSize fail.."<
- }
- return buf.st_size;
- }
-
🍎.判断文件是否存在
- bool sjp::FileUtil::Exist(){
- struct stat buf;
- if(stat(filename.c_str(),&buf)==-1){
- return false;
- }
- return true;
- }
🍎.获取文件最后一次进入时间
- size_t sjp::FileUtil::GetFileAccessTime(){
- struct stat buf;
- if(stat(filename.c_str(),&buf)==-1){
- cout<<"GetFielAccessTime fail.."<
- }
- return buf.st_atime;
- }
-
🍎.获取文件最后一次修改时间
- size_t sjp::FileUtil::GetFileModfityTime(){
- struct stat buf;
- if(stat(filename.c_str(),&buf)==-1){
- cout<<"GetFileSize fail.."<
- }
- return buf.st_mtime;
- }
🌳.获取文件名
功能:
FIleUtil中filename存储的时路径,GetFilename的功能仅仅获取文件名,例如文件的路径是wwwroot/filename,获取文件名就是filename.
- std::string sjp::FileUtil::GetFilename(){
- size_t pos=filename.rfind("/");
- if(pos==std::string::npos)
- {
- return filename;
- }
- return filename.substr(pos+1);
- }
-
🌳.获取文件的内容
功能:
GetPosContent 获取FileUtil中的文件中从pos位置到pos+n的数据,获取到的文件数据存储在body中.
- //获取文件的部分内容
- bool sjp::FileUtil::GetPosContent(size_t pos,size_t n,std::string& body){
- if(!Exist()){
- cout<<"GetPostContent: file is not exist"<
- return false;
- }
-
- if(GetFileSize()
- cout<<"GetFileSize fail:42"<
- return false;
- }
- //按照二进制流的方式打开文件
- std::ifstream ifs;
- ifs.open(filename,std::ios::binary);
- //将文件位置偏移到pos位置
- ifs.seekg(pos,std::ios::beg);
- body.resize(n);
- //获取到的数据放进body中
- ifs.read(&body[0],n);
- if(ifs.good()==false){
- std::cout<<"get file content failed\n"<
- return false;
- }
- ifs.close();
- return true;
- }
-
- //获取文件的所有的内容
- bool sjp::FileUtil::GetContent(std::string& body){
- size_t size=GetFileSize();
- return GetPosContent(0,size,body);
- }
- ifstream对象能够将文件中的数据关联起来,并可以读取文件中的内容。
- ifs.seekg(pos,std::ios::beg) :将文件从起始位置偏移到pos的位置
🌳.往文件中写入内容
功能:
SetContent将body中的数据写入到FileUtil关联的文件中。
- bool sjp::FileUtil::SetContent(std::string& body)
- {
- //打开文件将文件与ofs关联起来
- std::ofstream ofs;
- ofs.open(filename,std::ios::binary);
- //将body的数据写入文件中
- ofs.write(body.c_str(),body.size());
- if(ofs.good()==false)
- {
- cout<<"FileUtil.cpp 52 line:SetContent failing"<
- return false;
- }
- return true;
- }
-
- ofstream对象能够与文件进行关联起来,并可以往文件中写入数据
🌳.获取目录下所有文件的相对路径
功能:
如果FileUtil存储的是一个目录,GetPathInDir可以获取到目录下的所有文件的相对路径。
- #include
- namespace fs = std::experimental::filesystem;
- bool sjp::FileUtil::GetPathInDir(std::vector
& arry) - {
- for(auto & p:fs::directory_iterator(filename)){
- arry.push_back(p.path().relative_path());
- }
- return true;
- }
-
- 获取目录下所有文件信息,需要使用c++17中的目录迭代器,目录迭代器能够目录下所有的文件信息.
- 迭代器获取到的文件信息存放在directory_entry对象里面,在directory_entry类中包括一个path的类型的变量,path类里面有一个relative_path接口。这个接口是获取文件的相对路径
- 使用c++17的目录迭代器需要包含filesystem文件。
- 如果使用g++编译器,需要在编译指令中添加-lstdc++fs,例如:
g++ -o main main.cc FileUtil.cpp -std=c++11 -lstdc++fs -g
🌳.压缩文件
功能:
将FileUtil对象中的文件进行压缩,压缩的文件为packname
- bool sjp::FileUtil::PackFile(const std::string& packname)
- {
- //将打开文件
- std::ifstream ifs;
- ifs.open(filename.c_str(),std::ios::binary);
- ifs.seekg(0,ifs.end);
- int size=ifs.tellg();
- ifs.seekg(0,ifs.beg);
-
- //将文件信息往提取到文件中
- std::string body;
- body.resize(size);
- ifs.read(&body[0],size);
-
- //压缩文件
- std::string packed=bundle::pack(bundle::LZIP,body);
- std::ofstream ofs;
- ofs.open(packname.c_str());
- ofs.write(&packed[0],packed.size());
- ofs.close();
- ifs.close();
- return true;
- }
压缩文件需要使用bundle库中接口,bundle的具体使用在下面这篇文章中有详细解析:
🌳.解压文件
功能:
将FileUtil对象中的文件进行解压缩,压缩的文件为unpackfile.
- bool sjp::FileUtil::UnpackFile(const std::string& unpackfile){
- std::ifstream ifs;
- ifs.open(filename.c_str(),std::ios::binary);
- ifs.seekg(0,ifs.end);
- int size=ifs.tellg();
- ifs.seekg(0,ifs.beg);
-
- std::string body;
- body.resize(size);
- ifs.read(&body[0],size);
- std::string unpacked=bundle::unpack(body);
-
- std::ofstream ofs;
- ofs.open(unpackfile.c_str(),std::ios::binary);
- ofs.write(unpacked.c_str(),unpacked.size());
- ifs.close();
- ofs.close();
- return true;
- }
解压缩文件也是需要使用bundle库中接口。
🌳.删除文件
功能:
删除FileUtil中的文件
- bool sjp::FileUtil::Remove(){
- remove(filename.c_str());
- return true;
- }
🚀.Json工具类
Json工具类的设计需要使用到Jsoncpp接口,Jsoncpp接口和Json的详解在这篇文章有详细解析:
【Json】——jsoncpp的序列化以及反序列化
- Serialize将Json::Value对象root序列化 Json字符串body。
- UnSerialize将Json字符串body反序列化为Json::Value对象root.
- class JsonUtil
- {
- public:
- //将root序列化为Json字符串
- static bool Serialize(Json::Value& root,std::string& body)
- {
- Json::StreamWriterBuilder swb;
- std::unique_ptr
s(swb.newStreamWriter()) ; - std::stringstream ss;
- //将value对象转换为Json数据流
- s->write(root,&ss);
- body=ss.str();
- return true;
- }
-
- //将Json字符串反序列化为Json::Value
- static bool UnSerialize(Json::Value& root,std::string& body)
- {
- Json::CharReaderBuilder crb;
- std::unique_ptr
cr(crb.newCharReader()) ; - std::string err;
- W> bool ret=cr->parse(body.c_str(),body.c_str()+body.size(),&root,&err);
- return true;
- }
- };
-
相关阅读:
通过对抽象模型和概念模型的整合,细化项目整体流程
查找2个dom节点的最近的共同父元素
C++多线程学习06
DL之GRU(Tensorflow框架):基于茅台股票数据集利用GRU算法实现回归预测(保存模型.ckpt.index、.ckpt.data文件)
私募基金CRM客户关系管理系统软件开发应该具备的功能清单
全栈技术面试十问(中英双语)
Unity关于无法新建项目的可能解决办法
YB506A是一款锂电池充、放电管理专用芯片,集成锂电池充电管理和降压DCDC电路
一、基本remix环境及HelloWord contract《2022 solidity8.+ 版本教程到实战》
Nacos-server(1.4.4)安装部署-SpringCloudAlibaba微服务
-
原文地址:https://blog.csdn.net/sjp11/article/details/128169780
-
最新文章
-
【FHE】我们如何实现同态加密推理(八):SiLU 的密文化——两条路径,和一个 8 字节的开关
SQLcl 中 c/old/new 替换变量为何失效?先定位再替换
不让模型写作文,直接从它脑子里读答案:Jev 决策在 .NET 的两条路线
Halo插件:支持GitHub的图床附件策略
电视投屏界的“一股清流”!SimpleTVCast 6.0.0-beta 抢先体验:无需TV端App,界面大换血!✨
华三VXLAN集中式网关实战指南:Spine多活网关 + 双活接入(M-LAG)
软件更好写之后口碑会更重要
大 O、大 Omega、大 Theta 的区别,O(n) 复杂度是什么意思
Java标签跳转:一个鲜为人知但却很实用的小技巧
多智能体系统论文速读