C++文件路径处理1 - 判断一个文件是否存在|是否可读|是否可写|是否可执行

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。

posted @ 2024-06-27 22:15  陌尘(MoChen)  阅读(2)  评论(0编辑  收藏  举报