在线文档教程
C++
文件系统 | Filesystem

std::filesystem::path

std::filesystem::path

Defined in header
class path;(since C++17)

Objects of type path 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:

  • root-name(optional): identifies the root on a filesystem with multiple roots (such as "C:" or "//myserver"). POSIX filesystems have single root.

A path in normal form has no redundant current directory (dot) elements, no redundant parent directory (dot-dot) elements, and no redundant directory-separators. The normal form for an empty path is an empty path. The normal form for a path ending in a directory-separator that is not the root directory has a current directory (dot) element appended.

The path can be traversed element-wise via iterators returned by the begin() and end() functions, which 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 a file name dot.

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

If the OS uses a native syntax that is different from the portable generic syntax described above, all library functions accept path names in both formats.

Paths are implicitly convertible to and from std::basic_strings, which makes it possible to use them with over files APIs, e.g. as an argument to std::ifstream::open.

Member types

TypeDefinition
value_typecharacter type used by the native encoding of the filesystem: char on POSIX, wchar_t on Windows
string_typestd::basic_string<value_type>
const_iteratora constant BidirectionalIterator with a value_type of path
iteratoran alias to const_iterator

Member constants

constexpr value_type preferred_separator staticalternative 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)

Member functions

(constructor)constructs a path (public member function)
(destructor)destroys a path object (public member function)
operator=assigns another path (public member function)
assignassigns contents (public member function)

| Concatenation |

| appendoperator/= | appends elements to the path with a directory separator (public member function) |

| concatoperator+= | concatenates two paths without introducing a directory separator (public member function) |

| Modifiers |

| clear | erases the contents (public member function) |

| make_preferred | converts directory separators to preferred directory separator (public member function) |

| remove_filename | removes filename path component (public member function) |

| replace_filename | replaces the last path component with another path (public member function) |

| replace_extension | replaces the extension (public member function) |

| swap | swaps two paths (public member function) |

| Format observers |

| c_strnativeoperator string_type | returns the native version of the path (public member function) |

| stringwstringu8stringu16stringu32string | returns the path in native pathname format converted to a string (public member function) |

| generic_stringgeneric_wstringgeneric_u8stringgeneric_u16stringgeneric_u32string | returns the path in generic pathname format converted to a string (public member function) |

| Compare |

| compare | compares the lexical representations of two paths lexicographically (public member function) |

| Generation |

| lexically_normallexically_relativelexically_proximate | converts path to normal formconverts path to relative formconverts path to proximate form (public member function) |

| Decomposition |

| root_name | returns the root-name of the path, if present (public member function) |

| root_directory | returns the root directory of the path, if present (public member function) |

| root_path | returns the root path of the path, if present (public member function) |

| relative_path | returns path relative to the root path (public member function) |

| parent_path | returns the path of the parent path (public member function) |

| filename | returns the filename path component (public member function) |

| stem | returns the stem path component (public member function) |

| extension | returns the file extension path component (public member function) |

| Queries |

| empty | checks if the path is empty (public member function) |

| has_root_pathhas_root_namehas_root_directoryhas_relative_pathhas_parent_pathhas_filenamehas_stemhas_extension | checks if the corresponding path element is not empty (public member function) |

| is_absoluteis_relative | checks if root_path() uniquely identifies file system location (public member function) |

| Iterators |

| beginend | iterator access to the path as a sequence of elements (public member function) |

Non-member functions

swap(std::filesystem::path)swaps two paths (function)
hash_valuecalculates a hash value for a path object (function)
operator==operator!=operator<operator<=operator>operator>=lexicographically compares two paths (function)
operator/concatenates two paths with a directory separator (function)
operator<<operator>>performs stream input and output on a path (function)
u8path (C++17)creates a path from a UTF-8 encoded source (function)

© cppreference.com

Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.

http://en.cppreference.com/w/cpp/filesystem/path