std::filesystem::exists(3)
π 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
sbe astd::filesystem::file_statusdetermined as if bystatus(p)orstatus(p, ec)(symlinks are followed), respectively. Returnsexists(s). The non-throwing overload callsec.clear()ifstatus_known(s).
π Parameters π
sβ file status to checkpβ path to examineecβ 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
noexceptmay throwstd::bad_allocif memory allocation fails. - 2) 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. - 3) 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.
π¨ 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 ofstd::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
- 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