C/C++ Windows 与 Unix 平台上面使用 access 访问文件函数

在 Windows 与 Unix 平台上面得 C/C++ 之中,都标准提供了 access 函数得实现,只不过参数会有一些不同。

为了确保跨平台编译、兼容得通用、及一致性,所以人们需要显示定义:

#if defined(_WIN32)
#include <io.h>
#else
#include <unistd.h>
#endif

#ifndef R_OK
#define R_OK 4 /* Test for read permission. */
#endif

#ifndef W_OK
#define W_OK 2 /* Test for write permission. */
#endif

#ifndef X_OK
#define X_OK 1 /* Test for execute permission. */
#endif

#ifndef F_OK
#define F_OK 0 /* Test for existence. */
#endif

当我们期望通过 access 函数来判定文件是否存在时,可以实现以下得函数:

        bool File::Exists(const char* path) noexcept {
            if (NULL == path) {
                return false;
            }

            return access(path, F_OK) == 0;
        }

 

鉴于 Windows 与 Unix 平台上面,对于 access 函数参数得宏值定义不同,所以人们需要定义一个内部枚举。

        enum FileAccess {
            Read                                = 1,
            Write                               = 2,
            ReadWrite                           = 3,
        };

其后,在通过定义得内部枚举,根据行为得不同实现具体得事务。

        bool File::CanAccess(const char* path, FileAccess access_) noexcept {
#if defined(_WIN32)
            if (NULL == path) {
                return false;
            }

            int flags = 0;
            if ((access_ & FileAccess::ReadWrite) == FileAccess::ReadWrite) {
                flags |= R_OK | W_OK;
            }
            else {
                if (access_ & FileAccess::Read) {
                    flags |= R_OK;
                }
                if (access_ & FileAccess::Write) {
                    flags |= W_OK;
                }
            }
            return access(path, flags) == 0;
#else
            int flags = 0;
            if ((access_ & FileAccess::ReadWrite) == FileAccess::ReadWrite) {
                flags |= O_RDWR;
            }
            else {
                if (access_ & FileAccess::Read) {
                    flags |= O_RDONLY;
                }
                if (access_ & FileAccess::Write) {
                    flags |= O_WRONLY;
                }
            }

            int fd = open(path, flags);
            if (fd == -1) {
                return false;
            }
            else {
                close(fd);
                return true;
            }
#endif
        }

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-10 09:58:02       99 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-10 09:58:02       107 阅读
  3. 在Django里面运行非项目文件

    2024-07-10 09:58:02       90 阅读
  4. Python语言-面向对象

    2024-07-10 09:58:02       98 阅读

热门阅读

  1. 贪心算法-以高校教材管理系统为例

    2024-07-10 09:58:02       26 阅读
  2. 使用 .NET 实现 MongoDB

    2024-07-10 09:58:02       33 阅读
  3. ES5/ES6补充笔记

    2024-07-10 09:58:02       23 阅读
  4. Conda Channels全掌握:Linux中添加与移除的艺术

    2024-07-10 09:58:02       36 阅读
  5. Jetson-AGX-Orin离线安装nvidia-jetpack

    2024-07-10 09:58:02       25 阅读
  6. 2024前端面试真题【CSS篇】

    2024-07-10 09:58:02       28 阅读
  7. 如何使用echart画k线图

    2024-07-10 09:58:02       29 阅读
  8. 【国产开源可视化引擎】Meta2d.js简介

    2024-07-10 09:58:02       31 阅读
  9. 【C语言】常见的数据排序算法

    2024-07-10 09:58:02       29 阅读