LIB-ZC, 一个跨平台(Linux)平台通用C/C++扩展库, 文件操作

LIB-ZC, 一个跨平台(Linux)平台通用C/C++扩展库, 文件操作

文件相关的方法:

  • 主要是为了解决跨平台的问题
  • 其次对一些常见操作做了封装

注意

  • 下面介绍的函数的命名空间为 zcc
  • 一般来说, 如果和标准C库同名,则参数和返回值的意义一样
  • 文件名路径名如无特殊说明则为 UTF-8

打开文件

FILE *fopen(const char *pathname, const char *mode);
FILE *fopen(const std::string &pathname, const char *mode);

文件名实际路径

std::string realpath(const char *pathname);

文件大小

int64_t file_get_size(const char *pathname);
int64_t file_get_size(const std::string &pathname);

文件是否存储在

// @return int 0, -1, 1
int file_exists(const char *pathname);

向文件写内容

// @return int 失败返回 -1
int file_put_contents(const char *pathname, const void *data, int len);
int file_put_contents(const std::string &pathname, const void *data, int len);
int file_put_contents(const char *pathname, const std::string &data);
int file_put_contents(const std::string &pathname, const std::string &data);

读取文件内容

std::string file_get_contents(const char *pathname);
std::string file_get_contents(const std::string &pathname);
// 失败时返回 -1
int64_t file_get_contents(const char *pathname, std::string &bf);
int64_t file_get_contents(const std::string &pathname, std::string &bf);
int64_t file_get_contents_sample(const char *pathname, std::string &bf);
// 测试模式, 文件不存在, 则程序退出
std::string file_get_contents_sample(const char *pathname);
// 从标准输入读
int stdin_get_contents(std::string &bf);
std::string stdin_get_contents();

open/touch/…

int open(const char *pathname, int flags, int mode);
int open(const std::string &pathname, int flags, int mode);
int touch(const char *pathname);
int rename(const char *oldpath, const char *newpath);
int unlink(const char *pathname);
int unlink(const std::string &pathname);
int link(const char *oldpath, const char *newpath);
int link_force(const char *oldpath, const char *newpath, const char *tmpdir);
int symlink(const char *oldpath, const char *newpath);
int symlink_force(const char *oldpath, const char *newpath, const char *tmpdir);

创建快捷方式

bool create_shortcut_link(const char *from, const char *to);
bool create_shortcut_link(const std::string &from, const std::string &to);

创建目录

int mkdir(const char *pathname, int mode = 0666);
// 循环创建目录
int mkdir(std::vector<std::string> paths, int mode = 0666);
// 循环创建目录
int mkdir(int mode, const char *path1, ...);

删除目录

// @param pathname 目录的路径名。
// @param recurse_mode 是否递归删除,默认为 false。
// @return int 操作成功返回 0,失败返回 -1。
int rmdir(const char *pathname, bool recurse_mode = false);
int rmdir(const std::string &pathname, bool recurse_mode = false);

文件夹遍历

int scandir(const char *dirname, std::vector<dir_item_info> &filenames);
int scandir(const std::string &dirname, std::vector<dir_item_info> &filenames);
std::vector<dir_item_info> scandir(const char *dirname);
std::vector<dir_item_info> scandir(const std::string &dirname);
// Linux平台, 测试环境下, find 系统命令封装
std::vector<std::string> find_file_sample(std::vector<const char *> dir_or_file, const char *pathname_match = nullptr);
std::vector<std::string> find_file_sample(const char **dir_or_file, int item_count, const char *pathname_match = nullptr);

文件名工具

// 格式化文件名, 把非法字符转为 _
std::string format_filename(const char *filename);
std::string format_filename(const std::string &filename);
// 拼接路径
std::string path_concat(const char *path1, ...);
// 获取文件的目录
std::string get_dirname(const char *pathname);
// 获取文件的目录和文件名
void get_dirname_and_filename(const char *pathname, std::string &dirname, std::string &filename);
void get_dirname_and_filename(const std::string &pathname, std::string &dirname, std::string &filename);
// 导出文件, 自动括号里数字递增
std::string get_pathname_for_dump(const char *dirname, const char *filename, int max_loop = -1);
std::string get_pathname_for_dump(const std::string &dirname, const std::string &filename, int max_loop = -1);
get_pathname_for_dump(const char *pathname, int max_loop = -1);
std::string get_pathname_for_dump(const std::string &pathname, int max_loop = -1);

其他

// 兼容 windows平台
int64_t getdelim(char **lineptr, int64_t *n, int delim, FILE *stream);
int64_t getline(char **lineptr, int64_t *n, FILE *stream);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值