Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::filesystem::u8path

      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
       
      Defined in header<filesystem>
      template<class Source>
      std::filesystem::path u8path(const Source& source);
      (1)(since C++17)
      (deprecated in C++20)
      template<class InputIt>
      std::filesystem::path u8path( InputIt first, InputIt last);
      (2)(since C++17)
      (deprecated in C++20)

      Constructs a pathp from a UTF-8 encoded sequence ofchars orchar8_ts(since C++20), supplied either as anstd::string, or asstd::string_view, or as a null-terminated multibyte string, or as a[first, last) iterator pair.

      • Ifpath::value_type ischar and native encoding is UTF-8, constructs a path directly as if bypath(source) orpath(first, last). Note: this is the typical situation of a POSIX system that uses Unicode, such as Linux.
      • Otherwise, ifpath::value_type iswchar_t and native encoding is UTF-16 (this is the situation on Windows), or ifpath::value_type ischar16_t (native encoding guaranteed UTF-16) orchar32_t (native encoding guaranteed UTF-32), then first converts the UTF-8 character sequence to a temporary stringtmp of typepath::string_type and then the new path is constructed as if bypath(tmp).
      • Otherwise (for non-UTF-8 narrow character encodings and for non-UTF-16wchar_t), first converts the UTF-8 character sequence to a temporary UTF-32-encoded stringtmp of typestd::u32string, and then the new path is constructed as if bypath(tmp) (this path is taken on a POSIX system with a non-Unicode multibyte or single-byte encoded filesystem).

      Contents

      [edit]Parameters

      source - a UTF-8 encodedstd::string,std::string_view, a pointer to a null-terminated multibyte string, or an input iterator with char value type that points to a null-terminated multibyte string
      first, last - pair ofLegacyInputIterators that specify a UTF-8 encoded character sequence
      Type requirements
      -
      InputIt must meet the requirements ofLegacyInputIterator.
      -
      The value type ofSource orInputIt must becharorchar8_t.(since C++20)

      [edit]Return value

      The path constructed from the input string after conversion from UTF-8 to the filesystem's native character encoding.

      [edit]Exceptions

      May throwstd::bad_alloc if memory allocation fails.

      [edit]Notes

      On systems where native path format differs from the generic path format (neither Windows nor POSIX systems are examples of such OSes), if the argument to this function is using generic format, it will be converted to native.

      [edit]Example

      Run this code
      #include <cstdio>#ifdef _MSC_VER#include <fcntl.h>#include <io.h>#else#include <clocale>#include <locale>#endif#include <filesystem>#include <fstream> int main(){#ifdef _MSC_VER    _setmode(_fileno(stderr), _O_WTEXT);#elsestd::setlocale(LC_ALL,"");std::locale::global(std::locale(""));#endif std::filesystem::path p(u8"要らない.txt");std::ofstream(p)<<"File contents";// Prior to LWG2676 uses operator string_type()// on MSVC, where string_type is wstring, only// works due to non-standard extension.// Post-LWG2676 uses new fstream constructors // Native string representation can be used with OS-specific APIs#ifdef _MSC_VERif(std::FILE* f= _wfopen(p.c_str(), L"r"))#elseif(std::FILE* f=std::fopen(p.c_str(),"r"))#endif{for(int ch;(ch= fgetc(f))!=EOF;std::putchar(ch)){}std::fclose(f);} std::filesystem::remove(p);}

      Possible output:

      File contents

      [edit]See also

      (C++17)
      represents a path
      (class)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/filesystem/path/u8path&oldid=158113"

      [8]ページ先頭

      ©2009-2025 Movatter.jp