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

FreeBSD Manual Pages

  
 
  

home | help
std::filesy...try::exists(3)  C++ Standard Libary std::filesy...try::exists(3)

NAME
       std::filesystem::directory_entry::exists	   -   std::filesystem::direc-
       tory_entry::exists

Synopsis
	  bool exists()	const;				      (since C++17)
	  bool exists( std::error_code&	ec ) const noexcept;

	  Checks whether the pointed-to	object exists. Effectively returns
	  std::filesystem::exists(status())  or	  std::filesystem::exists(sta-
       tus(ec)),
	  respectively.

Parameters
	  ec - out-parameter for error reporting in the	non-throwing overload

Return value
	  true if the referred-to filesystem object exists.

Exceptions
	  The overload that does not take a std::error_code& parameter throws
	  filesystem::filesystem_error	on  underlying	OS  API	 errors,  con-
       structed	with p as the
	  first	path argument and the OS error code as the  error  code	 argu-
       ment. 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.

Example
       // Run this code

	#include <filesystem>
	#include <fstream>
	#include <iostream>
	#include <string>

	namespace fs = std::filesystem;

	int main()
	{
	    // store current path to restore it	at exit
	    const auto old_current_path	= fs::current_path();

	    // create "sanbox" directory in temp dir
	    const auto dir_sandbox = fs::temp_directory_path() / "sandbox";

	    if (!fs::create_directory(dir_sandbox)) {
		std::cout << "ERROR #1"	<< '\n';
		return -1;
	    }

	    fs::current_path(dir_sandbox); // switch to	newly created dir

	    fs::directory_entry	entry_sandbox {	dir_sandbox };
	    if (!entry_sandbox.exists()) {
		std::cout << "ERROR #2"	<< '\n';
		return -1;
	    }

	    std::cout << "Current dir: " << entry_sandbox.path().filename() <<
       '\n';

	    fs::path path_tmp_file = dir_sandbox / "tmp_file";

	    std::ofstream  file(  path_tmp_file.string()  ); //	create regular
       file
	    file << "cppreference.com";	// write 16 bytes
	    file.flush();

	    fs::directory_entry	entry_tmp_file{	path_tmp_file };

	    if (entry_tmp_file.exists()) {
		std::cout << "File " <<	entry_tmp_file.path().filename()
			  << " has size: "  <<	entry_tmp_file.file_size()  <<
       '\n';
	    } else {
		std::cout << "ERROR #3"	<< '\n';
	    }

	    // cleanup
	    fs::current_path(old_current_path);
	    fs::remove_all(dir_sandbox);
	}

Possible output:
	Current	dir: "sandbox"
	File "tmp_file"	has size: 16

See also
	  exists  checks whether path refers to	existing file system object
	  (C++17) (function)

http://cppreference.com		  2022.07.31	  std::filesy...try::exists(3)

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

home | help