C++文件路径处理1 - 判断一个文件是否存在|是否可读|是否可写|是否可执行
- 1. 关键词
- 2. filesystem.h
- 3. filesystem_unix.cpp
- 4. filesystem_win.cpp
- 5. filepath.h & filepath.cpp
- 6. 测试代码
- 7. 运行结果
- 8. 源码地址
1. 关键词
关键词:
C++ 文件路径处理 文件 是否存在 是否可读 是否可写 是否可执行 跨平台
应用场景:
在对文件进行操作之前,对文件的访问权限进行判断。
2. filesystem.h
#pragma once #include <string> namespace cutl { bool file_exists(const std::string &filepath); bool file_readable(const std::string &filepath); bool file_writable(const std::string &filepath); bool file_executable(const std::string &filepath); } // namespace cutl
3. filesystem_unix.cpp
#if defined(_WIN32) || defined(__WIN32__) // do nothing #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_exists(const std::string &filepath) { return (access(filepath.c_str(), 0) == 0); } bool file_readable(const std::string &filepath) { return (access(filepath.c_str(), R_OK) == 0); } bool file_writable(const std::string &filepath) { return (access(filepath.c_str(), W_OK) == 0); } bool file_executable(const std::string &filepath) { return (access(filepath.c_str(), X_OK) == 0); } } // namespace cutl #endif // defined(_WIN32) || defined(__WIN32__)
4. 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 { // https://learn.microsoft.com/zh-cn/cpp/c-runtime-library/reference/access-waccess?view=msvc-170 bool file_exists(const std::string &filepath) { return (_access(filepath.c_str(), 0) == 0); } // https://learn.microsoft.com/zh-cn/cpp/c-runtime-library/reference/access-waccess?view=msvc-170 bool file_readable(const std::string &filepath) { // 04: 只读, 06: 读取和写入 return (_access(filepath.c_str(), 4) == 0) || (_access(filepath.c_str(), 6) == 0); } bool file_writable(const std::string &filepath) { // 02: 只写, 06: 读取和写入 return (_access(filepath.c_str(), 2) == 0) || (_access(filepath.c_str(), 6) == 0); } bool file_executable(const std::string &filepath) { CUTL_WARN("executable() is not supported on Windows"); return false; } } // namespace cutl #endif // defined(_WIN32) || defined(__WIN32__)
5. filepath.h & filepath.cpp
参考:
https://gitee.com/spencer_luo/common_util/blob/master/src/common_util/filepath.h
https://gitee.com/spencer_luo/common_util/blob/master/src/common_util/filepath.cpp
6. 测试代码
#include "common.hpp" #include "fileutil.h" void TestPermission() { PrintSubTitle("TestPermission"); auto path = cutl::path("./fileutil_test/file4.data"); std::cout << "path: " << path << ", exists: " << path.exists() << std::endl; std::cout << "path: " << path << ", readable: " << path.readable() << std::endl; std::cout << "path: " << path << ", writable: " << path.writable() << std::endl; std::cout << "path: " << path << ", executable: " << path.executable() << std::endl; auto path2 = cutl::path("./script/build.sh"); std::cout << "path2: " << path2 << ", executable: " << path2.executable() << std::endl; }
7. 运行结果
-------------------------------------------TestPermission------------------------------------------- path: ./fileutil_test/file4.data, exists: 1 path: ./fileutil_test/file4.data, readable: 1 path: ./fileutil_test/file4.data, writable: 1 path: ./fileutil_test/file4.data, executable: 0 path2: ./script/build.sh, executable: 1
8. 源码地址
更多详细代码,请查看本人写的C++ 通用工具库: common_util, 本项目已开源,代码简洁,且有详细的文档和Demo。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战