std::filesy...try::exists(3)
π 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:
std::filesystem::exists(status()),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).
- throws
std::filesystem::filesystem_erroron underlying OS API errors, constructed withpas the first path argument and the OS error code as the error code argument. - sets a
std::error_code¶meter to the OS API error code if an OS API call fails, and executesec.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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms