Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::filesystem::path::make_preferred

      From cppreference.com
      <cpp‎ |filesystem‎ |path
       
       
      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
       
      path& make_preferred();
      (since C++17)

      Converts all directory separators in the generic-format view of the path to the preferred directory separator.

      For example, on Windows, where\ is the preferred separator, the pathfoo/bar will be converted tofoo\bar.

      Contents

      [edit]Parameters

      (none)

      [edit]Return value

      *this

      [edit]Exceptions

      May throw implementation-defined exceptions.

      [edit]Example

      Windows can use/ as a separator, but prefers\, somake_preferred converts the forward slashes to backslashes. On the other hand, POSIX does not use\ as a separator, because backslashes are valid filename characters — the Windows path on POSIX actually refers to a file with the name"a\\b\\c". For this reason the "separators" are not converted.

      Run this code
      #include <filesystem>#include <iostream> int main(){std::filesystem::path        windows_path("a\\b\\c"),        posix_path("a/b/c"); std::cout<<"Windows path: "<< windows_path<<" -> "<< windows_path.make_preferred()<<'\n'<<"POSIX path: "<< posix_path<<" -> "<< posix_path.make_preferred()<<'\n';}

      Output:

      // on WindowsWindows path: "a\\b\\c" -> "a\\b\\c"POSIX path: "a/b/c" -> "a\\b\\c" // on POSIXWindows path: "a\\b\\c" -> "a\\b\\c"POSIX path: "a/b/c" -> "a/b/c"

      [edit]See also

      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]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/filesystem/path/make_preferred&oldid=154538"

      [8]ページ先頭

      ©2009-2025 Movatter.jp