std::filesystem::directory_entry::exists
From cppreference.com
< cpp | filesystem | directory entry
bool exists() const; bool exists( std::error_code& ec ) const noexcept; |
(since C++17) | |
Checks whether the pointed-to object exists. Effectively returns std::filesystem::exists(status()) or std::filesystem::exists(status(ec)), respectively.
Parameters
ec | - | out-parameter for error reporting in the non-throwing overload |
Return value
true if the referred-to filesystem object exists.
Exceptions
The overload that does not take a std::error_code& parameter throws 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. The overload taking a std::error_code& parameter sets it to the OS API error code if an OS API call fails, and executes ec.clear() if no errors occur. Any overload not marked noexcept
may throw std::bad_alloc if memory allocation fails.
Example
Run this code
#include <filesystem> #include <fstream> #include <iostream> #include <string> namespace fs = std::filesystem; int main() { // store current path to restore it at exit const auto old_current_path = fs::current_path(); // create "sanbox" directory in temp dir const auto dir_sandbox = fs::temp_directory_path() / "sandbox"; if (!fs::create_directory(dir_sandbox)) { std::cout << "ERROR #1" << '\n'; return -1; } fs::current_path(dir_sandbox); // switch to newly created dir fs::directory_entry entry_sandbox { dir_sandbox }; if (!entry_sandbox.exists()) { std::cout << "ERROR #2" << '\n'; return -1; } std::cout << "Current dir: " << entry_sandbox.path().filename() << '\n'; fs::path path_tmp_file = dir_sandbox / "tmp_file"; std::ofstream file( path_tmp_file.string() ); // create regular file file << "cppreference.com"; // write 16 bytes file.flush(); fs::directory_entry entry_tmp_file{ path_tmp_file }; if (entry_tmp_file.exists()) { std::cout << "File " << entry_tmp_file.path().filename() << " has size: " << entry_tmp_file.file_size() << '\n'; } else { std::cout << "ERROR #3" << '\n'; } // cleanup fs::current_path(old_current_path); fs::remove_all(dir_sandbox); }
Possible output:
Current dir: "sandbox" File "tmp_file" has size: 16
See also
(C++17) |
checks whether path refers to existing file system object (function) |