std::experi...stem::exists(3)
π NAME π
okay bestie, welcome to std::experi...stem::exists(3) β yes, the title is literally truncated like that β from the C++ Standard Libary manual (spelling exactly as it appears here, no cap). The thing being documented is std::experimental::filesystem::exists, and the NAME line is the same name twice: std::experimental::filesystem::exists - std::experimental::filesystem::exists. iconic. π
β¨ Synopsis β¨
Defined in header <experimental/filesystem> π«Ά
bool exists( file_status s ) (1) (filesystem TS)
bool exists( const path& p ); (2) (filesystem TS)
bool exists( const path& p, error_code& ec )
This function checks if the given file status or path corresponds to an existing file or directory. Here's the breakdown:
- Equivalent to
status_known(s) && s.type() != file_type::not_found. - Equivalent to
exists(status(p))orexists(status(p, ec))(symlinks are followed). The non-throwing overload returnsfalseif an error occurs.
πΈ 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. periodt. β¨
π₯ Exceptions π₯
noexceptspecification:noexcept.- The overload that does not take an
error_code¶meter throwsfilesystem_erroron underlying OS API errors, constructed withpas the first argument and the OS error code as the error code argument.std::bad_allocmay be thrown if memory allocation fails. The overload taking anerror_code¶meter sets it to the OS API error code if an OS API call fails, and executesec.clear()if no errors occur. This overload hasnoexceptspecification:noexcept.
π‘ 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()). iykyk π
π Example π
The original page says // Run this code before the snippet, so here it is in all its glory:
#include <cstdint>
#include <experimental/filesystem>
#include <fstream>
#include <iostream>
namespace fs = std::experimental::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()
{
fs::create_directory("sandbox");
std::ofstream("sandbox/file"); // create regular file
fs::create_symlink("non-existing", "sandbox/symlink");
demo_exists("sandbox");
for (auto it = fs::directory_iterator("sandbox"); it != fs::directory_iterator(); ++it)
demo_exists(*it, it->status()); // use cached status from directory entry
fs::remove_all("sandbox");
}
Output:
"sandbox" exists
"sandbox/file" exists
"sandbox/symlink" does not exist
π See also π
statusβ determines file attributessymlink_statusβ determines file attributes, checking the symlink target (function)file_statusβ represents file type and permissions (class)statusβ cached status of the file designated by this directory entrysymlink_statusβ cached symlink_status of the file designated by this directory entry (public member function of std::experimental::filesystem::directory_entry)
π Categories π
- Noindexed pages
- unconditionally noexcept
π Hidden categories π
- Pages with unreviewed unconditional noexcept template
- Pages with unreviewed noexcept template
mwah π β this iconic page came from http://cppreference.com, version/date 2024.06.10, and it's literally std::experi...stem::exists(3). 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