std::experimental::filesystem::exists
Min standard notice:
Header: <experimental/filesystem>
Checks if the given file status or path corresponds to an existing file or directory.
# Declarations
bool exists( file_status s )
(filesystem TS)
bool exists( const path& p );
bool exists( const path& p, error_code& ec )
(filesystem TS)
# Parameters
s: file status to checkp: path to examineec: 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.
# 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
#include <cstdint>
#include <experimental/filesystem>
#include <fstream>
#include <iostream>
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");
}