命名空间
变体
操作

std::filesystem::file_status::permissions

来自 cppreference.com
 
 
 
 
std::filesystem::perms permissions() const noexcept;
(1) (自 C++17)
void permissions( std::filesystem::perms perm ) noexcept;
(2) (自 C++17)

访问文件权限信息。

1) 返回文件权限信息。
2) 将文件权限设置为 perm.

[编辑] 参数

perm - 要设置的文件权限

[编辑] 返回值

1) 文件权限信息。
2) (无)

[编辑] 示例

#include <filesystem>
#include <fstream>
#include <iostream>
 
void demo_perms(std::filesystem::perms p)
{
    using std::filesystem::perms;
    auto show = [=](char op, perms perm)
    {
        std::cout << (perms::none == (perm & p) ? '-' : op);
    };
    show('r', perms::owner_read);
    show('w', perms::owner_write);
    show('x', perms::owner_exec);
    show('r', perms::group_read);
    show('w', perms::group_write);
    show('x', perms::group_exec);
    show('r', perms::others_read);
    show('w', perms::others_write);
    show('x', perms::others_exec);
    std::cout << '\n';
}
 
int main()
{
    std::ofstream("test.txt"); // create file
 
    std::cout << "Created file with permissions: ";
    demo_perms(std::filesystem::status("test.txt").permissions());
 
    std::filesystem::permissions(
        "test.txt",
        std::filesystem::perms::owner_all | std::filesystem::perms::group_all,
        std::filesystem::perm_options::add
    );
 
    std::cout << "After adding u+rwx and g+rwx:  ";
    demo_perms(std::filesystem::status("test.txt").permissions());
 
    std::filesystem::remove("test.txt");
}

可能的输出

Created file with permissions: rw-r--r--
After adding u+rwx and g+wrx:  rwxrwxr--