std::filesystem::exists (3) - Linux Manuals
std::filesystem::exists: std::filesystem::exists
Command to display std::filesystem::exists
manual in Linux: $ man 3 std::filesystem::exists
NAME
std::filesystem::exists - std::filesystem::exists
Synopsis
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 ) 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) 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.
Exceptions
2) The overload that does not take a std::error_code& parameter throws 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.
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())
Example
This section is incomplete
Reason: switch to directory_entry::exists
// Run this code
#include <iostream>
#include <fstream>
#include <cstdint>
#include <filesystem>
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()
{
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 attributes
symlink_status determines file attributes, checking the symlink target
(function)
(C++17)
(C++17)
file_status represents file type and permissions
(class)
(C++17)
checks whether directory entry refers to existing file system object
exists (public member function of std::filesystem::directory_entry)
Pages related to std::filesystem::exists
- std::filesystem::equivalent (3) - std::filesystem::equivalent
- std::filesystem::absolute (3) - std::filesystem::absolute
- std::filesystem::begin(directory_iterator),std::filesystem::end(directory_iterator) (3) - std::filesystem::begin(directory_iterator),std::filesystem::end(directory_iterator)
- std::filesystem::begin(recursive_directory_iterator), (3) - std::filesystem::begin(recursive_directory_iterator),
- std::filesystem::begin(recursive_directory_iterator),std::filesystem::end(recursive_directory_iterator) (3) - std::filesystem::begin(recursive_directory_iterator),std::filesystem::end(recursive_directory_iterator)
- std::filesystem::canonical,std::filesystem::weakly_canonical (3) - std::filesystem::canonical,std::filesystem::weakly_canonical
- std::filesystem::copy (3) - std::filesystem::copy
- std::filesystem::copy_file (3) - std::filesystem::copy_file
- std::filesystem::copy_options (3) - std::filesystem::copy_options
- std::filesystem::copy_symlink (3) - std::filesystem::copy_symlink
- std::filesystem::create_directory,std::filesystem::create_directories (3) - std::filesystem::create_directory,std::filesystem::create_directories
- std::filesystem::create_hard_link (3) - std::filesystem::create_hard_link
- std::filesystem::create_symlink,std::filesystem::create_directory_symlink (3) - std::filesystem::create_symlink,std::filesystem::create_directory_symlink
- std::filesystem::current_path (3) - std::filesystem::current_path
- std::filesystem::directory_entry (3) - std::filesystem::directory_entry
- std::filesystem::directory_entry::assign (3) - std::filesystem::directory_entry::assign
- std::filesystem::directory_entry::directory_entry (3) - std::filesystem::directory_entry::directory_entry
- std::filesystem::directory_entry::exists (3) - std::filesystem::directory_entry::exists
- std::filesystem::directory_entry::file_size (3) - std::filesystem::directory_entry::file_size
- std::filesystem::directory_entry::hard_link_count (3) - std::filesystem::directory_entry::hard_link_count
- std::filesystem::directory_entry::is_block_file (3) - std::filesystem::directory_entry::is_block_file