C++文件路径处理1 - 判断一个文件是否存在|是否可读|是否可写|是否可执行
1. 关键词
关键词:
C++ 文件路径处理 文件 是否存在 是否可读 是否可写 是否可执行 跨平台
应用场景:
在对文件进行操作之前,对文件的访问权限进行判断。
2. filesystem.h
1 2 3 4 5 6 7 8 9 10 11
| #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); }
|
3. filesystem_unix.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| #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_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); } }
#endif
|
4. filesystem_win.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| #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_exists(const std::string &filepath) { return (_access(filepath.c_str(), 0) == 0); }
bool file_readable(const std::string &filepath) { return (_access(filepath.c_str(), 4) == 0) || (_access(filepath.c_str(), 6) == 0); }
bool file_writable(const std::string &filepath) { 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; } }
#endif
|
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. 测试代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| #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. 运行结果
1 2 3 4 5 6
| -------------------------------------------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。