std::filesy...try::exists(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… std::filesystem::directory_entry::exists

okay bestie, welcome to the std::filesystem::directory_entry::exists page β€” straight out of the C++ Standard Libary manual (yes that's how the manual spells it, we don't gatekeep spelling here 🫢). This is the one that answers the most iconic question of all time: does this thing even exist? πŸ’€


✨ NAME ✨

std::filesystem::directory_entry::exists β€” std::filesystem::directory_entry::exists. that's it. that's the whole name. she's her own name twin, main character energy only πŸ‘‘


🌸 Synopsis 🌸

bool exists() const;                               (1) (since C++17)
bool exists( std::error_code& ec ) const noexcept; (2) (since C++17)

so what she does: she checks whether the pointed-to object exists ✨. effectively, she returns:

  1. std::filesystem::exists(status()),
  2. std::filesystem::exists(status(ec)).
⚠️ lowkey important: status() follows symlinks to their targets. so you're not asking "does the link exist", you're asking about the thing it points at. iykyk πŸ¦‹

πŸŽ€ Parameters πŸŽ€

  • ec β€” the out-parameter for error reporting in the non-throwing overload. like a little bucket where the error info goes if anything goes wrong πŸ™ˆ

⭐ Return value ⭐

true if the referred-to filesystem object exists. that's it, that's the vibe check πŸ”₯


πŸ’œ Exceptions πŸ’œ

any overload not marked noexcept may throw std::bad_alloc if memory allocation fails 😭 (rude, but ok).

  1. throws std::filesystem::filesystem_error on underlying OS API errors, constructed with p as the first path argument and the OS error code as the error code argument.
  2. sets a std::error_code& parameter to the OS API error code if an OS API call fails, and executes ec.clear() if no errors occur. so like, she cleans up after herself πŸ’…

🧚 Example 🧚

run this code fr fr:

#include <filesystem>
#include <iostream>

int main()
{
    for (auto const str:
    {
        "/usr/bin/cat",
        "/usr/bin/mouse",
        "/usr/bin/python",
        "/usr/bin/bison",
        "/usr/bin/yacc",
        "/usr/bin/c++",
    })
    {
        std::filesystem::directory_entry entry{str};

        std::cout << "directory entry " << entry
                  << (entry.exists() ? " exists\n" : " does not exist\n");
    }
}

🌈 Possible output 🌈

output on a POSIX system, bestie:

directory entry "/usr/bin/cat" exist
directory entry "/usr/bin/mouse" does not exist
directory entry "/usr/bin/python" exists
directory entry "/usr/bin/bison" exists
directory entry "/usr/bin/yacc" does not exist
directory entry "/usr/bin/c++" exists

(not me noticing the first line says exist and the rest say exists 😭 that's literally in the original, i don't make the rules πŸ’€)


πŸ¦‹ See also πŸ¦‹

  • exists β€” checks whether path refers to existing file system object (C++17) (function) ✨

okay pookie that's the whole page, you ate that 🫢 go forth and check if things exist πŸ’–

source: http://cppreference.com Β· version date: 2024.06.10 Β· this was std::filesy...try::exists(3) β€” mwah, bye bestie πŸ‘‹πŸ’…

πŸ“¦ where this page comes from