std::experimental::filesystem::exists (3) - Linux Manuals
std::experimental::filesystem::exists: std::experimental::filesystem::exists
Command to display std::experimental::filesystem::exists
manual in Linux: $ man 3 std::experimental::filesystem::exists
NAME
std::experimental::filesystem::exists - std::experimental::filesystem::exists
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 )
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) Equivalent to exists(status(p)) or exists(status(p, ec)) (symlinks are followed). The non-throwing overload returns false if an error occurs.
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
1)
noexcept specification:
noexcept
2) The overload that does not take a error_code& parameter throws filesystem_error on underlying OS API errors, constructed with p as the first argument and the OS error code as the error code argument. std::bad_alloc may be thrown if memory allocation fails. The overload taking a 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. This overload has
noexcept specification:
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())
Example
// Run this code
#include <iostream>
#include <fstream>
#include <cstdint>
#include <experimental/filesystem>
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
determines file attributes
status determines file attributes, checking the symlink target
symlink_status (function)
represents file type and permissions
file_status (class)
cached status of the file designated by this directory entry
status cached symlink_status of the file designated by this directory entry
symlink_status (public member function of std::experimental::filesystem::directory_entry)
Pages related to std::experimental::filesystem::exists
- std::experimental::filesystem::equivalent (3) - std::experimental::filesystem::equivalent
- std::experimental::filesystem::absolute,std::experimental::filesystem::system_complete (3) - std::experimental::filesystem::absolute,std::experimental::filesystem::system_complete
- std::experimental::filesystem::begin(directory_iterator), (3) - std::experimental::filesystem::begin(directory_iterator),
- std::experimental::filesystem::begin(directory_iterator),std::experimental::filesystem::end(directory_iterator) (3) - std::experimental::filesystem::begin(directory_iterator),std::experimental::filesystem::end(directory_iterator)
- std::experimental::filesystem::begin(recursive_directory_iterator), (3) - std::experimental::filesystem::begin(recursive_directory_iterator),
- std::experimental::filesystem::begin(recursive_directory_iterator),std::experimental::filesystem::end(recursive_directory_iterator) (3) - std::experimental::filesystem::begin(recursive_directory_iterator),std::experimental::filesystem::end(recursive_directory_iterator)
- std::experimental::filesystem::canonical (3) - std::experimental::filesystem::canonical
- std::experimental::filesystem::copy (3) - std::experimental::filesystem::copy
- std::experimental::filesystem::copy_file (3) - std::experimental::filesystem::copy_file
- std::experimental::filesystem::copy_options (3) - std::experimental::filesystem::copy_options
- std::experimental::filesystem::copy_symlink (3) - std::experimental::filesystem::copy_symlink
- std::experimental::filesystem::create_directory, (3) - std::experimental::filesystem::create_directory,
- std::experimental::filesystem::create_directory,std::experimental::filesystem::create_directories (3) - std::experimental::filesystem::create_directory,std::experimental::filesystem::create_directories
- std::experimental::filesystem::create_hard_link (3) - std::experimental::filesystem::create_hard_link
- std::experimental::filesystem::create_symlink, (3) - std::experimental::filesystem::create_symlink,
- std::experimental::filesystem::create_symlink,std::experimental::filesystem::create_directory_symlink (3) - std::experimental::filesystem::create_symlink,std::experimental::filesystem::create_directory_symlink
- std::experimental::filesystem::current_path (3) - std::experimental::filesystem::current_path
- std::experimental::filesystem::directory_entry (3) - std::experimental::filesystem::directory_entry
- std::experimental::filesystem::directory_entry::assign (3) - std::experimental::filesystem::directory_entry::assign
- std::experimental::filesystem::directory_entry::directory_entry (3) - std::experimental::filesystem::directory_entry::directory_entry
- std::experimental::filesystem::directory_entry::operator= (3) - std::experimental::filesystem::directory_entry::operator=
- std::experimental::filesystem::directory_entry::operator==,!=,<,<=,>,>= (3) - std::experimental::filesystem::directory_entry::operator==,!=,<,<=,>,>=
- std::experimental::filesystem::directory_entry::path (3) - std::experimental::filesystem::directory_entry::path
- std::experimental::filesystem::directory_entry::replace_filename (3) - std::experimental::filesystem::directory_entry::replace_filename
- std::experimental::filesystem::directory_entry::status, (3) - std::experimental::filesystem::directory_entry::status,
- std::experimental::filesystem::directory_entry::status,std::experimental::filesystem::directory_entry::symlink_status (3) - std::experimental::filesystem::directory_entry::status,std::experimental::filesystem::directory_entry::symlink_status
- std::experimental::filesystem::directory_iterator (3) - std::experimental::filesystem::directory_iterator
- std::experimental::filesystem::directory_iterator::directory_iterator (3) - std::experimental::filesystem::directory_iterator::directory_iterator
- std::experimental::filesystem::directory_iterator::operator*,operator-> (3) - std::experimental::filesystem::directory_iterator::operator*,operator->
- std::experimental::filesystem::directory_iterator::operator= (3) - std::experimental::filesystem::directory_iterator::operator=
- std::experimental::filesystem::directory_options (3) - std::experimental::filesystem::directory_options
- std::experimental::filesystem::file_size (3) - std::experimental::filesystem::file_size
- std::experimental::filesystem::file_status (3) - std::experimental::filesystem::file_status
- std::experimental::filesystem::file_status::file_status (3) - std::experimental::filesystem::file_status::file_status
- std::experimental::filesystem::file_status::operator= (3) - std::experimental::filesystem::file_status::operator=