Skip site navigation (1)Skip section navigation (2)

FreeBSD Manual Pages

  
 
  

home | help
std::experi...stem::exists(3) C++ Standard Libarystd::experi...stem::exists(3)

NAME
       std::experimental::filesystem::exists   -   std::experimental::filesys-
       tem::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))	 (sym-
       links 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 argu-
       ment 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(*it-
       erator) 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_sta-
       tus{})
	{
	    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::direc-
       tory_iterator();	++it)
		demo_exists(*it, it->status());	// use cached status from  di-
       rectory 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 tar-
       get
			 (function)
	  file_status	 represents file type and permissions
			 (class)
			 cached	 status	 of the	file designated	by this	direc-
       tory entry
	  status	 cached	symlink_status of the file designated by  this
       directory entry
	  symlink_status (public member	function of
			 std::experimental::filesystem::directory_entry)

http://cppreference.com		  2022.07.31	 std::experi...stem::exists(3)

Want to link to this manual page? Use this URL:
<https://man.freebsd.org/cgi/man.cgi?query=std::experimental::filesystem::exists&sektion=3&manpath=FreeBSD+Ports+15.0>

home | help