目录
🌳.文件工具类的介绍
🌳.文件工具类的功能简介
🌳.stat接口
🍎. 获取文件的大小
🍎.判断文件是否存在
🍎.获取文件最后一次进入时间
🍎.获取文件最后一次修改时间
🌳.获取文件名
🌳.获取文件的内容
🌳.往文件中写入内容
🌳.获取目录下所有文件的相对路径
🌳.压缩文件
🌳.解压文件
🌳.删除文件
文件工具类主要是创建一个文件对象将磁盘上的一个文件关联起来,方便对该文件进行操作和获取文件的信息等操作,包括获取文件的内容,文件的大小,文件的访问时间,往文件中写入内容等操作。
文件的 成员变量是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();//删除文件//压缩文件,压缩文件名为packnamebool PackFile(const std::string&packname);//解压缩文件,解压缩文件名为packnamebool 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.."<
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.."<
size_t sjp::FileUtil::GetFileModfityTime(){ struct stat buf; if(stat(filename.c_str(),&buf)==-1){ cout<<"GetFileSize fail.."<
功能:
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"<
功能:
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"<
功能:
如果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;
}
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的具体使用在下面这篇文章中有详细解析:
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;
}