前言

  • 自己常用的工具库, C++ 和C语言实现
  • 使用cmake维护的项目
  • 持续更新.....
  • 提供使用范例, 详见example文件夹
  • windows使用的VS通过了的编译。 Linux(Ubuntu)使用的是 clang++ 10.0
  • 欢迎留言交流

下载地址

giteegithub

文件读写接口


/// ----------------------------------------------------------------------------------------
/// 文件读写接口
/// ----------------------------------------------------------------------------------------
class ifile
{
public: /// --------------------------------------------------------------------------------
/// @brief: 初始化
/// @str_file - 文件所在绝对路径, 例如: C:/demo/logs/1.txt
/// @open_mode - 以哪种方式打开文件
/// @return - int
/// 0 - 成功
/// 1 - 失败, 参数【str_file】字符串为空
/// 2 - 失败,参数【open_mode】不是给定的枚举范围
/// 3 - 失败,文件打开失败。
/// --------------------------------------------------------------------------------
virtual int init_(const std::string& str_file, const oct_toolkits::en_file_open_mode open_mode) = 0; /// --------------------------------------------------------------------------------
/// 返回当前初始化的文件
/// @return - std::string
///
/// --------------------------------------------------------------------------------
virtual std::string file_() = 0; /// --------------------------------------------------------------------------------
/// 文件是否打开
/// @return - bool
/// true - 打开
/// false - 没有打开
/// --------------------------------------------------------------------------------
virtual bool is_opened_() = 0; /// --------------------------------------------------------------------------------
/// 返回文件长度,单位: 字节
/// @return - int
/// 返回值X,
/// -2 - 失败,请先执行初始化
/// X >=0 , 文件长度,单位:字节
/// --------------------------------------------------------------------------------
virtual int length_() = 0; /// --------------------------------------------------------------------------------
/// 写文件, 将数据写入
/// @pbuf - 待写入内容
/// @pbuf_len - 待写入数据长度
/// @return - int
/// 0 - 成功
/// 1 - 失败,请先初始化
/// 2 - 失败,参数【pbuf】为nullptr或者NULL
/// 3 - 失败,写文件失败
/// --------------------------------------------------------------------------------
virtual int write_(const char* pbuf, unsigned int pbuf_len) = 0;
/// --------------------------------------------------------------------------------
/// 写文件
/// @str_write - 待文件内容
/// @return - int
/// 0 - 成功
/// 1 - 失败,请先初始化
/// 2 - 失败,参数【str_write】的长度为0
/// 3 - 失败,写文件失败
/// --------------------------------------------------------------------------------
virtual int write_(const std::string& str_write) = 0; /// --------------------------------------------------------------------------------
/// 将文件内容儒道参数【str_read】中
/// @str_read - 返回文件内容
/// @return - int
/// 0 - 成功
/// 1 - 失败,请先初始化
/// 2 - 失败,读取文件缓冲创建失败
/// 3 - 失败,读取文件失败
/// --------------------------------------------------------------------------------
virtual int read_(std::string& str_read) = 0; /// --------------------------------------------------------------------------------
/// 读文件内容, 参数【pout_buf】需要外部申请,函数内不会维护,读取成功后,文件内容存放在pout_buf中。
/// @pout_buf - 返回读取文件内容
/// @pout_buf_len - 【pout_buf】缓冲区的长度
/// @return - int
/// 0 - 成功
/// 1 - 失败,请先初始化
/// 2 - 失败,参数【pout_buf】为空 或者 参数【pout_buf_len】的值是0
/// --------------------------------------------------------------------------------
virtual int read_(char* pout_buf, unsigned int pout_buf_len) = 0; /// --------------------------------------------------------------------------------
/// 判断参数【str_check_file】的文件是否存在
/// @str_check_file -
/// @return - bool
///
/// --------------------------------------------------------------------------------
virtual bool is_existed_(const std::string& str_check_file) = 0; /// --------------------------------------------------------------------------------
/// 避免析构时出现异常,请销毁前先调用该函数完成内部资源释放
/// @return - int
/// 0 - 成功
/// --------------------------------------------------------------------------------
virtual int uninit_() = 0;
};

日志库接口

	/// ----------------------------------------------------------------------------------------
/// 日志文件接口类
/// ----------------------------------------------------------------------------------------
class ilog
{
public:
/// --------------------------------------------------------------------------------
/// 初始化,
/// @info - 日志信息
/// @return - int
/// 0 - 成功
/// 1 - 失败,日志文件路径不正确
/// 2 - 失败,如果磁盘空间剩余不够,则禁止创建日志文件对象
/// 3 - 失败,无法创建日志文件路径, 无法写入日志
/// 5 - 失败,创建日志文件失败,则无法写入日志
/// --------------------------------------------------------------------------------
virtual int init_(const oct_toolkits::st_log_info& info) = 0; /// --------------------------------------------------------------------------------
/// 写日志,日志内容为文本,且每一行前面都带有时间; 例如: [2021-03-28 15:00:00:001] 日志文件内容
/// @str_log - 待写入日志文件内容
/// @return - int
/// 0 - 成功
/// 1 - 无法写入日志,可能是日志文件创建失败,或者磁盘剩余空间不足5G
/// 2 - 失败,无法创建写日志文件对象,请先初始化
/// --------------------------------------------------------------------------------
virtual int log_text_(const std::string& str_log) = 0; /// --------------------------------------------------------------------------------
/// 写日志
/// @pdata - 待写入内容
/// @pdata_len - 待写入长度
/// @return - int
/// 0 - 成功、
/// 1 - 失败,参数【pdata】为空或者参数【pdata_len】为0
/// 2 - 失败,无法写入日志,可能是日志文件创建失败,或者磁盘剩余空间不足5G
/// 3 - 失败,无法写入,日志文件读写对象创建失败,请先初始化
/// --------------------------------------------------------------------------------
virtual int log_text_(const char* pdata, unsigned int pdata_len) = 0; /// --------------------------------------------------------------------------------
/// 将参数【str_log】每个字节的16进制写入文件,全部大写
/// @str_log - 待写入内容
/// @return - int
/// 0 - 成功
/// 1 - 无法写入日志,可能是日志文件创建失败,或者磁盘剩余空间不足5G
/// 2 - 失败,无法写入,日志文件读写对象创建失败,请先初始化
/// --------------------------------------------------------------------------------
virtual int log_hex_(const std::string& str_log) = 0; /// --------------------------------------------------------------------------------
/// 将参数【pdata】每个字节的16进制写入文件,全部大写
/// @pdata - 待写入内容
/// @pdata_len - 写入内容长度
/// @return - int
/// 0 - 成功
/// 1 - 失败,参数【pdata】为空或者参数【pdata_len】为0
/// 2 - 失败,无法写入日志,可能是日志文件创建失败,或者磁盘剩余空间不足5G
/// 3 - 失败,无法写入,日志文件读写对象创建失败,请先初始化
/// --------------------------------------------------------------------------------
virtual int log_hex_(const char* pdata, unsigned int pdata_len) = 0; /// --------------------------------------------------------------------------------
/// 释放内部资源
/// @return - void
///
/// --------------------------------------------------------------------------------
virtual void uninit_() = 0;
};

其他常用接口

	/// ----------------------------------------------------------------------------------------
/// 常用工具
/// ----------------------------------------------------------------------------------------
class lib_toolkits_api toolkits
{
public:
explicit toolkits();
virtual ~toolkits(); /// --------------------------------------------------------------------------------
/// 返回年月日时分秒毫秒字符串
/// @return - std::string
///
/// --------------------------------------------------------------------------------
st_now_date get_date_now_(); /// --------------------------------------------------------------------------------
/// 格式化字符串
/// @pformat - 格式化字符串
/// @ - 参数列表
/// @return - std::string
/// 返回格式化结果
/// --------------------------------------------------------------------------------
std::string str_format_(const char*pformat, ...); /// --------------------------------------------------------------------------------
/// 获取可执行程序所在绝对路径
/// @return - std::string
///
/// --------------------------------------------------------------------------------
std::string get_executable_dir_(); /// -----------------------------------------------------------------
/// windows特有的转码函数, 如果是非Windows平台调用,则返回空的字符串
/// ----------------------------------------------------------------- /// --------------------------------------------------------------------------------
/// @brief: utf8字符串转ascii
/// @param: const std::string & str_utf8 - utf8字符串编码
/// @return: std::string
/// ascii字符串编码
/// --------------------------------------------------------------------------------
std::string utf8_to_ascii_(const std::string& str_utf8); /// --------------------------------------------------------------------------------
/// @brief: ascii字符编码转utf8字符串
/// @param: const std::string & str_acsii - acsii编码字符串
/// @return: std::string
/// utf8字符串
/// --------------------------------------------------------------------------------
std::string acsii_to_utf8_(const std::string& str_acsii); private:
/// 屏蔽拷贝构造函数
toolkits(const toolkits& instance) {}
toolkits& operator =(const toolkits& instance) { return *this; }
};

C++常用工具库(C语言文件读写,日志库,格式化字符串, 获取可执行文件所在绝对路径等)的更多相关文章

  1. c语言文件读写操作总结

    C语言文件读写操作总结 C语言文件操作 一.标准文件的读写 1.文件的打开 fopen() 文件的打开操作表示将给用户指定的文件在内存分配一个FILE结构区,并将该结构的指针返回给用户程序,以后用户程 ...

  2. 3,C语言文件读写

    这两天看到一个关于文件读写的题目,索性就把相关内容总结下. C语言文件读写,无非是几个读写函数的应用,fopen(),fread(),fwrite()等,下面简单介绍下. 一.fopen() 函数原型 ...

  3. C语言文件读写命令fprintf和fscanf

    以向文件中读取和写入二维数组为例. 以下是fprintf的使用:向文件中写入10*10的二维数组,数组元素为1~100之间的随机数. #include <stdlib.h> #includ ...

  4. swift调用oc语言文件,第三方库文件或者自己创建的oc文件——简书作者

    Swift是怎样调用OC的第三方库的呢?请看下面详情: 情况一: 1.首先打开Xcode,iOS->Application->Single View Application, 选Next. ...

  5. [知识复习] C语言文件读写

    文件打开 fopen() 返回FILE* 对象,如果打开失败返回NULL,错误代码存入errno中 FILE *fopen( const char * filename, const char * m ...

  6. C语言文件读写操作

    C语言实现文件读写,注意区分几个方法: 写入: fwrite() //个人认为这个最好,可是实现写入任何数据类型,任何长度 fputs() //写入一个字符串,字符串长度不能太长,具体的长度未知,但估 ...

  7. 一、Git的一些命令操作----创建版本库、增加文件到Git库、时光机穿梭、远程仓库

    具体详细教程请链接:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 我这里只是记录 ...

  8. Go语言系列之日志库zap

    在许多Go语言项目中,我们需要一个好的日志记录器能够提供下面这些功能: 能够将事件记录到文件中,而不是应用程序控制台. 日志切割-能够根据文件大小.时间或间隔等来切割日志文件. 支持不同的日志级别.例 ...

  9. C++标准库实现WAV文件读写

    在上一篇文章RIFF和WAVE音频文件格式中对WAV的文件格式做了介绍,本文将使用标准C++库实现对数据为PCM格式的WAV文件的读写操作,只使用标准C++库函数,不依赖于其他的库. WAV文件结构 ...

随机推荐

  1. 洛谷 P3266 - [JLOI2015]骗我呢(容斥原理+组合数学)

    题面传送门 神仙题. 首先乍一看此题非常棘手,不过注意到有一个条件 \(0\le x_{i,j}\le m\),而整个矩阵恰好有 \(m\) 列,这就启发我们考虑将每个元素的上下界求出来,如果我们第一 ...

  2. 【豆科基因组】绿豆Mungbean, Vigna radiata苏绿基因组预印

    目录 一.来源 二.结果 测序组装 组装评价 编码基因预测 基因功能注释 非编码RNA注释 假基因预测 重复序列注释 进化分析和分歧时间估计 全基因组复制 LTR插入时间估计 正选择基因 一.来源 H ...

  3. Metabolomics Workfolw

    推荐R语言界的国内大佬于淼写的代谢组学workflow,包含了大部分代谢组学(以及暴露组)的数据分析方法. Meta-Workflow 主要内容包括: Sample collection Pretre ...

  4. 27-Roman to Integer-Leetcode

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  5. javaSE高级篇1 — 异常与多线程基础

    1.异常的体系结构  注:Throwable是一个类,不是一个接口,这个类里面是描述的一些Error和Exception的共性,如图所示: 异常 / 错误是什么意思? 定义:指的是程序运行过程中,可能 ...

  6. Yarn 生产环境核心参数配置案例

    目录 Yarn 生产环境核心参数配置案例 需求 修改yarn-site.xml配置 分发 重启集群 执行WordCount程序 Yarn 生产环境核心参数配置案例 调整下列参数之前要拍摄Linux快照 ...

  7. 日常Java 2021/10/26

    HashSet基于HashMap来实现的,是一个不允许有重复元素的集合.HashSet 允许有null 值. HashSet是无序的,即不会记录插入的顺序. HashSet不是线程安全的,如果多个线程 ...

  8. A Child's History of England.29

    You have not forgotten the New Forest which the Conqueror made, and which the miserable people whose ...

  9. 内存管理——placement new

    C++给我们三个申请内存的方式,new(new operator),array new 和placement new. placement new意思是 让对象构建在已经分配好的内存上. (这里我再把 ...

  10. 简化版chmod

    我们知道对文件访问权限的修改在Shell下可通过chmod来进行 例如 可以看到v.c文件从无权限到所有者可读可写可执行.群组和其他用户可读可执行 chmod函数原型 int chmod(const ...