1. 关键词
C++ 文件系统操作 创建文件 删除文件 创建软连接 刪除软连接 跨平台
2. fileutil.h
| |
| #pragma once |
| |
| #include <string> |
| #include <cstdio> |
| #include <cstdint> |
| #include "filetype.h" |
| #include "filepath.h" |
| |
| namespace cutl |
| { |
| |
| |
| |
| |
| |
| class file_guard |
| { |
| public: |
| |
| |
| |
| |
| |
| explicit file_guard(FILE *file); |
| |
| |
| |
| |
| |
| ~file_guard(); |
| |
| |
| |
| |
| |
| |
| FILE *getfd() const; |
| |
| private: |
| FILE *file_; |
| }; |
| |
| |
| |
| |
| |
| |
| |
| bool createfile(const filepath &path); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| bool createlink(const filepath &referenece, const filepath &filepath); |
| |
| |
| |
| |
| |
| |
| |
| bool removefile(const filepath &path); |
| |
| } |
3. fileutil.cpp
| #include <cstdio> |
| #include <map> |
| #include <iostream> |
| #include <cstring> |
| #include <sys/stat.h> |
| #include "fileutil.h" |
| #include "inner/logger.h" |
| #include "inner/filesystem.h" |
| #include "strutil.h" |
| |
| namespace cutl |
| { |
| file_guard::file_guard(FILE *file) |
| : file_(file) |
| { |
| } |
| |
| file_guard::~file_guard() |
| { |
| if (file_) |
| { |
| |
| int ret = fclose(file_); |
| if (ret != 0) |
| { |
| CUTL_ERROR("fail to close file, ret" + std::to_string(ret)); |
| } |
| file_ = nullptr; |
| } |
| |
| } |
| |
| FILE *file_guard::getfd() const |
| { |
| return file_; |
| } |
| |
| bool createfile(const filepath &path) |
| { |
| auto dirPath = path.dirname(); |
| if (dirPath.empty()) |
| { |
| CUTL_ERROR("invalid path: " + path.str()); |
| return false; |
| } |
| if (!cutl::path(dirPath).exists()) |
| { |
| CUTL_ERROR("directory does not exist: " + dirPath); |
| return false; |
| } |
| |
| file_guard fg(fopen(path.str().c_str(), "w")); |
| if (fg.getfd() == nullptr) |
| { |
| CUTL_ERROR("fail to open file:" + path.str()); |
| return false; |
| } |
| |
| int ret = fflush(fg.getfd()); |
| if (0 != ret) |
| { |
| CUTL_ERROR("fail to flush file:" + path.str()); |
| return false; |
| } |
| |
| if (!file_sync(fg.getfd())) |
| { |
| CUTL_ERROR("file_sync failed for " + path.str()); |
| return false; |
| } |
| |
| return true; |
| } |
| |
| bool createlink(const filepath &referenece, const filepath &filepath) |
| { |
| return file_createlink(referenece.str(), filepath.str()); |
| } |
| |
| bool removefile(const filepath &path) |
| { |
| int ret = remove(path.str().c_str()); |
| if (ret != 0) |
| { |
| CUTL_ERROR("remove " + path.str() + " error, ret:" + std::to_string(ret)); |
| return false; |
| } |
| return true; |
| } |
4. filetype.h
| |
| #include <vector> |
| #include <string> |
| |
| #pragma once |
| |
| namespace cutl |
| { |
| |
| bool file_createlink(const std::string &referenece, const std::string &filepath); |
| } |
5. filesystem_win.cpp
| #if defined(_WIN32) || defined(__WIN32__) |
| |
| #include <io.h> |
| #include <direct.h> |
| #include <Windows.h> |
| #include <stdlib.h> |
| #include "strutil.h" |
| #include "filesystem.h" |
| #include "logger.h" |
| |
| namespace cutl |
| { |
| bool file_createlink(const std::string &referenece, const std::string &filepath) |
| { |
| CUTL_ERROR("file_createlink() is not supported on Windows"); |
| return false; |
| } |
| } |
| |
| #endif |
6. filesystem_unix.cpp
| #if defined(_WIN32) || defined(__WIN32__) |
| |
| #else |
| |
| #include <unistd.h> |
| #include <sys/stat.h> |
| #include <dirent.h> |
| #include <stack> |
| #include <cstring> |
| #include <utime.h> |
| #include <stdlib.h> |
| #include <sys/time.h> |
| #include "filesystem.h" |
| #include "inner/logger.h" |
| |
| namespace cutl |
| { |
| bool file_createlink(const std::string &referenece, const std::string &filepath) |
| { |
| int ret = ::symlink(referenece.c_str(), filepath.c_str()); |
| if (ret != 0) |
| { |
| CUTL_ERROR("symlink error. filepath:" + filepath + ", referenece:" + referenece + ", error:" + strerror(errno)); |
| return false; |
| } |
| return true; |
| } |
| } |
| |
| #endif |
7. 源码地址
更多详细代码,请查看本人写的C++ 通用工具库: common_util, 本项目已开源,代码简洁,且有详细的文档和Demo。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战