Search⌘ K
AI Features

File Permissions

Explore how to work with file permissions in C++17 using the std::filesystem library. Understand how to retrieve permissions, modify them with functions like status and permissions, and handle platform-specific considerations such as Windows' limited permission support.

Permission Functions

We have two major functions related to file permissions:

  • std::filesystem::status() and
  • std::filesystem::permissions()

The first one returns file_status which contains information about the file type and also its permissions.

And you can use the second function to modify the file permissions. For example, to change a file to be read-only.

std::filesystem::perms

File permissions - std::filesystem::perms - it’s an enum class that represents the following values:

Name Value (octal) POSIX macro Notes
none 0000 There are no permissions set for the file
owner_read 0400 S_IRUSR Read permission, owner
owner_write 0200 S_IWUSR Write permission, owner
...