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 a
std::filesystem::file_statusdetermined 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).
[edit] Parameters s - file status to check p - path to examine ec - out-parameter for error reporting in the non-throwing overload [edit] Return valuetrue if the given path or file status corresponds to an existing file or directory, false otherwise.
[edit] ExceptionsAny overload not marked noexcept
may throw std::bad_alloc if memory allocation fails.
Throws
std::filesystem::filesystem_erroron underlying OS API errors, constructed with
pas 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 executes
ec.clear()if no errors occur.
No filesystem exception is thrown if object does not exist (use return value).
[edit] NotesThe 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()).
[edit] Example#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[edit] See also determines file attributes
std::filesystem::directory_entry
) [edit]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4