欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 幼教 > LIB-ZC, 一个跨平台(Linux)平台通用C/C++扩展库, 文件操作

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

2026/5/31 22:29:16 来源:https://blog.csdn.net/eli960/article/details/147087170  浏览:    关键词: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);

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词