Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::filesystem::status: object type and permissions

std::filesystem::status reads the file_status of the object named by a path and follows symbolic links encountered during resolution.

Version: C++23

Syntax

std::filesystem::file_status std::filesystem::status(
    const std::filesystem::path& p);

std::filesystem::file_status std::filesystem::status(
    const std::filesystem::path& p, std::error_code& ec) noexcept;

std::filesystem::status(p) from <filesystem> returns a file_status containing a file_type and perms bits. Path resolution follows symlinks; use symlink_status to inspect the link itself. Predicates such as is_regular_file and is_directory accept the returned status.

The example creates a regular file, obtains status through the error_code overload, and prints 1 0: the type is regular and ec is clear. A status is only an observation at call time; another process can change or remove the file immediately afterward.

not_found means known absence, unknown means a resolved object whose attributes could not be determined, and none means an error prevented even that distinction. not_found and unknown do not make the throwing overload throw; the error_code& overload is noexcept, and ec must be inspected independently of the returned type.

Examples

#include <filesystem>
#include <fstream>
#include <iostream>

int main() {
    namespace fs = std::filesystem;
    const fs::path root = fs::temp_directory_path() / "cktis-cpp-ref-status";
    const fs::path file = root / "item.txt";
    std::error_code ignored;
    fs::remove_all(root, ignored);
    fs::create_directory(root);
    std::ofstream(file) << "data";

    std::error_code ec;
    const fs::file_status info = fs::status(file, ec);
    std::cout << fs::is_regular_file(info) << ' ' << static_cast<bool>(ec) << '\n';

    fs::remove_all(root, ignored);
}

Result

1 0

std::filesystem::status → 1 0

Sources