GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::filesystem::exists(3) C++ Standard Libary std::filesystem::exists(3)

std::filesystem::exists - std::filesystem::exists


Defined in header <filesystem>
bool exists( std::filesystem::file_status s ) noexcept; (1) (since C++17)
bool exists( const std::filesystem::path& p );
bool exists( const std::filesystem::path& p, std::error_code& ec ) (2) (since C++17)
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).


s - file status to check
p - path to examine
ec - out-parameter for error reporting in the non-throwing overload


true if the given path or file status corresponds to an existing file or directory,
false otherwise.


2) 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.


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()).

// 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()
{
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);
}


"sandbox" exists
"sandbox/symlink" does not exist
"sandbox/file" exists


status determines file attributes
symlink_status determines file attributes, checking the symlink target
(C++17) (function)
(C++17)
file_status represents file type and permissions
(C++17) (class)
exists checks whether directory entry refers to existing file system object
(public member function of std::filesystem::directory_entry)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.