Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::filesystem::path

      From cppreference.com
      <cpp‎ |filesystem
       
       
      Filesystem library
      Classes
      Functions
      File types
       
      std::filesystem::path
      Member types
      Member constants
      Member functions
      Path decomposition
      Non-member functions
      (until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
      Helper classes
       
      Defined in header<filesystem>
      class path;
      (since C++17)

      Objects of typepath represent paths on a filesystem. Only syntactic aspects of paths are handled: the pathname may represent a non-existing path or even one that is not allowed to exist on the current file system or OS.

      The path name has the following syntax:

      1. root-name(optional): identifies the root on a filesystem with multiple roots (such as"C:" or"//myserver"). In case of ambiguity, the longest sequence of characters that forms a validroot-name is treated as theroot-name. The standard library may define additional root-names besides the ones understood by the OS API.
      2. root-directory(optional): a directory separator that, if present, marks this path asabsolute. If it is missing (and the first element other than the root name is a file name), then the path isrelative and requires another path as the starting location to resolve to a file name.
      3. Zero or more of the following:
      • file-name: sequence of characters that aren't directory separators or preferred directory separators (additional limitations may be imposed by the OS or file system). This name may identify a file, a hard link, a symbolic link, or a directory. Two specialfile-names are recognized:
      • dot: the file name consisting of a single dot character. is a directory name that refers to the current directory.
      • dot-dot: the file name consisting of two dot characters.. is a directory name that refers to the parent directory.
      • directory-separators: the forward slash character/ or the alternative character provided aspath::preferred_separator. If this character is repeated, it is treated as a single directory separator:/usr///////lib is the same as/usr/lib.

      A path can benormalized by following this algorithm:

      1. If the path is empty, stop (normal form of an empty path is an empty path).
      2. Replace eachdirectory-separator (which may consist of multiple slashes) with a singlepath::preferred_separator.
      3. Replace each slash character in theroot-name withpath::preferred_separator.
      4. Remove eachdot and any immediately followingdirectory-separator.
      5. Remove each non-dot-dot filename immediately followed by adirectory-separator and adot-dot, along with any immediately followingdirectory-separator.
      6. If there isroot-directory, remove alldot-dots and anydirectory-separators immediately following them.
      7. If the last filename isdot-dot, remove any trailingdirectory-separator.
      8. If the path is empty, add adot (normal form of./ is.).

      The path can be traversed element-wise via iterators returned by thebegin() andend() functions, which views the path in generic format and iterates over root name, root directory, and the subsequent file name elements (directory separators are skipped except the one that identifies the root directory). If the very last element in the path is a directory separator, the last iterator will dereference to an empty element.

      Calling any non-const member function of apath invalidates all iterators referring to elements of that object.

      If the OS uses anative syntax that is different from the portablegeneric syntax described above, library functions that are defined to accept "detected format" accept path names in both formats: a detected format argument is taken to be in the generic format if and only if it matches the generic format but is not acceptable to the operating system as a native path. On those OS where native format differs between pathnames of directories and pathnames of files, a generic pathname is treated as a directory path if it ends on a directory separator and a regular file otherwise.

      In any case, the path class behaves as if it stores a pathname in the native format and automatically converts to generic format as needed (each member function specifies which format it interprets the path as).

      On POSIX systems, the generic format is the native format and there is no need to distinguish or convert between them.

      Paths are implicitly convertible to and fromstd::basic_strings, which makes it possible to use them with other file APIs.

      Thestream operators usestd::quoted so that spaces do not cause truncation when later read bystream input operator.

      Decomposition member functions (e.g.extension) returnfilesystem::path objects instead of string objects as other APIs do.

      Contents

      [edit]Member types

      Type Definition
      value_type character type used by the native encoding of the filesystem:char on POSIX,wchar_t on Windows
      string_typestd::basic_string<value_type>
      const_iterator a constantLegacyInputIterator with avalue_type ofpath that meets all requirements ofLegacyBidirectionalIterator except that for two equal dereferenceable iteratorsa andb of typeconst_iterator, there is no requirement that*a and*b refer to the same object.

      It is unspecified whetherconst_iterator is actually aLegacyBidirectionalIterator

      iteratorconst_iterator
      determines how to interpret string representations of pathnames.

      The following enumerators are also defined:

      Name Explanation
      native_format native pathname format
      generic_format generic pathname format
      auto_format implementation-defined format, auto-detected where possible

      (public member enum)

      [edit]Member constants

      constexpr value_type preferred_separator
      [static]
      alternative directory separator which may be used in addition to the portable/. On Windows, this is the backslash character\. On POSIX, this is the same forward slash/ as the portable separator
      (public static member constant)[edit]

      [edit]Member functions

      constructs apath
      (public member function)[edit]
      destroys apath object
      (public member function)[edit]
      assigns another path
      (public member function)[edit]
      assigns contents
      (public member function)[edit]
      Concatenation
      appends elements to the path with a directory separator
      (public member function)[edit]
      concatenates two paths without introducing a directory separator
      (public member function)[edit]
      Modifiers
      erases the contents
      (public member function)[edit]
      converts directory separators to preferred directory separator
      (public member function)[edit]
      removes filename path component
      (public member function)[edit]
      replaces the last path component with another path
      (public member function)[edit]
      replaces the extension
      (public member function)[edit]
      swaps two paths
      (public member function)[edit]
      Format observers
      returns the native version of the path
      (public member function)[edit]
      returns the path in native pathname format converted to a string
      (public member function)[edit]
      returns the path in generic pathname format converted to a string
      (public member function)[edit]
      Compare
      compares the lexical representations of two paths lexicographically
      (public member function)[edit]
      Generation
      converts path to normal form
      converts path to relative form
      converts path to proximate form
      (public member function)[edit]
      Decomposition
      returns the root-name of the path, if present
      (public member function)[edit]
      returns the root directory of the path, if present
      (public member function)[edit]
      returns the root path of the path, if present
      (public member function)[edit]
      returns path relative to the root path
      (public member function)[edit]
      returns the path of the parent path
      (public member function)[edit]
      returns the filename path component
      (public member function)[edit]
      returns the stem path component (filename without the final extension)
      (public member function)[edit]
      returns the file extension path component
      (public member function)[edit]
      Queries
      checks if the path is empty
      (public member function)[edit]
      checks if the corresponding path element is not empty
      (public member function)[edit]
      checks ifroot_path() uniquely identifies file system location
      (public member function)[edit]
      Iterators
      iterator access to the path as a sequence of elements
      (public member function)[edit]

      [edit]Non-member functions

      Defined in namespacestd::filesystem
      specializes thestd::swap algorithm
      (function)[edit]
      (C++17)
      calculates a hash value for a path object
      (function)[edit]
      (C++17)(C++17)(until C++20)(C++17)(until C++20)(C++17)(until C++20)(C++17)(until C++20)(C++17)(until C++20)(C++20)
      lexicographically compares two paths
      (function)[edit]
      (C++17)
      concatenates two paths with a directory separator
      (function)[edit]
      performs stream input and output on a quoted path
      (function)[edit]
      (C++17)(deprecated in C++20)
      creates apath from a UTF-8 encoded source
      (function)[edit]

      [edit]Helper classes

      Defined in namespacestd
      hash support forstd::filesystem::path
      (class template specialization)[edit]
      formatting support forfilesystem::path
      (class template specialization)[edit]

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3657C++17hash forpath was disabledenabled
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/filesystem/path&oldid=178501"

      [8]ページ先頭

      ©2009-2025 Movatter.jp