Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::experimental::filesystem::exists

      From cppreference.com
      <cpp‎ |experimental‎ |fs
       
       
       
      Filesystem library
      Classes
      Functions
      File types
       
      Defined in header<experimental/filesystem>
      bool exists( file_status s)
      (1)(filesystem TS)
      bool exists(const path& p);
      bool exists(const path& p, error_code& ec)
      (2)(filesystem TS)

      Checks if the given file status or path corresponds to an existing file or directory.

      1) Equivalent tostatus_known(s)&& s.type()!= file_type::not_found.
      2) Equivalent toexists(status(p)) orexists(status(p, ec)) (symlinks are followed). The non-throwing overload returnsfalse if an error occurs.

      Contents

      [edit]Parameters

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

      [edit]Return value

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

      [edit]Exceptions

      1)
      noexcept specification:  
      noexcept
        
      2) The overload that does not take anerror_code& parameter throwsfilesystem_error on underlying OS API errors, constructed withp 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 anerror_code& parameter sets it to the OS API error code if an OS API call fails, and executesec.clear() if no errors occur. This overload has
      noexcept specification:  
      noexcept
        

      [edit]Notes

      The information provided by this function is usually also provided as a byproduct of directory iteration. During directory iteration, callingexists(*iterator) is less efficient thanexists(iterator->status()).

      [edit]Example

      Run this code
      #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";elsestd::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

      [edit]See also

      determines file attributes
      determines file attributes, checking the symlink target
      (function)[edit]
      represents file type and permissions
      (class)[edit]
      cached status of the file designated by this directory entry
      cached symlink_status of the file designated by this directory entry
      (public member function ofstd::experimental::filesystem::directory_entry)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/experimental/fs/exists&oldid=159765"

      [8]ページ先頭

      ©2009-2025 Movatter.jp