Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::filesystem::exists

      From cppreference.com
      <cpp‎ |filesystem
       
       
      Filesystem library
      Classes
      Functions
      File types
       
      Defined in header<filesystem>
      bool exists(std::filesystem::file_status s)noexcept;
      (1)(since C++17)
      bool exists(conststd::filesystem::path& p);
      (2)(since C++17)
      bool exists(conststd::filesystem::path& p,std::error_code& ec)noexcept;
      (3)(since C++17)

      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,3) Lets be astd::filesystem::file_status determined as if bystatus(p) orstatus(p, ec) (symlinks are followed), respectively. Returnsexists(s). The non-throwing overload callsec.clear() ifstatus_known(s).

      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

      Any overload not markednoexcept may throwstd::bad_alloc if memory allocation fails.

      2) Throwsstd::filesystem::filesystem_error on underlying OS API errors, constructed withp as the first path argument and the OS error code as the error code argument.
      3) Sets astd::error_code& parameter to the OS API error code if an OS API call fails, and executesec.clear() if no errors occur.

      No filesystem exception is thrown if object does not exist (use return value).

      [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 <filesystem>#include <fstream>#include <iostream>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";elsestd::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(constauto& entry: fs::directory_iterator(sandbox))        demo_exists(entry, entry.status());// use cached status from directory entry     fs::remove_all(sandbox);}

      Output:

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

      [edit]See also

      (C++17)(C++17)
      determines file attributes
      determines file attributes, checking the symlink target
      (function)[edit]
      represents file type and permissions
      (class)[edit]
      checks whether directory entry refers to existing file system object
      (public member function ofstd::filesystem::directory_entry)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/filesystem/exists&oldid=159790"

      [8]ページ先頭

      ©2009-2025 Movatter.jp