std::filesystem::exists(3)

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

πŸ’– std::filesystem::exists(3) πŸ’–

okay bestie, welcome to the C++ Standard Libary page for std::filesystem::exists β€” yes, β€œLibary,” that’s literally the manual name up top, and we are keeping it because it’s iconic πŸ’….

This one is literally checking if your path or file status points at something that exists β€” file or directory, no cap.


🌸 NAME 🌸

std::filesystem::exists β€” std::filesystem::exists

like… that’s the whole NAME section. it’s just the function name doing a little mirror selfie. periodt.


πŸ“œ Synopsis πŸ“œ

It’s defined in header <filesystem> ✨

bool exists( std::filesystem::file_status s ) noexcept;
(1) (since C++17)
bool exists( const std::filesystem::path& p );
(2) (since C++17)
bool exists( const std::filesystem::path& p, std::error_code& ec )
(3) (since C++17)
noexcept;

Checks if the given file status or path corresponds to an existing file or directory.

  • 1) Equivalent to status_known(s) && s.type() != file_type::not_found.
  • 2,3) Let s be a std::filesystem::file_status determined as if by status(p) or status(p, ec) (symlinks are followed), respectively. Returns exists(s). The non-throwing overload calls ec.clear() if status_known(s).

πŸŽ€ Parameters πŸŽ€

  • s β€” file status to check
  • p β€” path to examine
  • ec β€” out-parameter for error reporting in the non-throwing overload

βœ… Return value βœ…

true if the given path or file status corresponds to an existing file or directory, false otherwise.

like, that’s it. that’s the vibe check. 🫢


😭 Exceptions 😭

  • Any overload not marked noexcept may throw std::bad_alloc if memory allocation fails.
  • 2) 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.
  • 3) 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.
🚨 No filesystem exception is thrown if the object does not exist β€” use the return value, bestie. don’t be dramatic about it.

🧚 Notes 🧚

The information provided by this function is usually also provided as a byproduct of directory iteration. During directory iteration, calling exists(*iterator) is less efficient than exists(iterator->status()).

so like… if you already have the status cached from the directory entry, use that. your future self will thank you πŸ’–


πŸ–₯️ Example πŸ–₯️

// Run this code

#include <cstdint>
#include <filesystem>
#include <fstream>
#include <iostream>
namespace fs = std::filesystem;

void demo_exists(const fs::path& p, fs::file_status s =
fs::file_status{})
{
    std::cout << p;
    if (fs::status_known(s) ? fs::exists(s) : fs::exists(p))
        std::cout << " exists\n";
    else
        std::cout << " does not exist\n";
}

int main()
{
    const fs::path sandbox{"sandbox"};
    fs::create_directory(sandbox);
    std::ofstream{sandbox/"file"}; // create regular file
    fs::create_symlink("non-existing", sandbox/"symlink");

    demo_exists(sandbox);

    for (const auto& entry : fs::directory_iterator(sandbox))
        demo_exists(entry, entry.status()); // use cached status from
directory entry

    fs::remove_all(sandbox);
}

✨ Output ✨

"sandbox" exists
"sandbox/symlink" does not exist
"sandbox/file" exists

πŸ‘€ See also πŸ‘€

  • status β€” determines file attributes; (C++17); (function)
  • symlink_status β€” determines file attributes, checking the symlink target; (C++17); (function)
  • file_status β€” represents file type and permissions; (C++17); (class)
  • exists β€” checks whether directory entry refers to existing file system object; public member function of std::filesystem::directory_entry

okay that’s the whole page, bestie πŸ’– source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::filesystem::exists(3) β€” now go check if your file exists and slay ✨

πŸ“¦ where this page comes from