std::experimental::filesystem::u8path
Min standard notice:
Header: <experimental/filesystem>
Constructs a path p from a UTF-8 encoded sequence of chars, supplied either as an std::string, or as a null-terminated multibyte string, or as a [first, last) iterator pair.
# Declarations
template< class Source >
path u8path( const Source& source );
(filesystem TS)
template< class InputIt >
path u8path( InputIt first, InputIt last );
(filesystem TS)
# Parameters
source: a UTF-8 encoded std::string, pointer to a null-terminated multibyte string, or an input iterator with char value type that points to a null-terminated multibyte stringfirst, last: pair of LegacyInputIterators that specify a UTF-8 encoded character sequence
# Return value
The path constructed from the input string after conversion from UTF-8 to the filesystem’s native character encoding.
# 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.
# Example
#include <clocale>
#include <cstdio>
#include <experimental/filesystem>
#include <fstream>
#include <iostream>
namespace fs = std::experimental::filesystem;
int main()
{
std::setlocale(LC_ALL, "en_US.utf8");
std::locale::global(std::locale("en_US.utf8"));
fs::path p = fs::u8path(u8"要らない.txt");
// native string representation can be used with OS APIs
std::ofstream(p) << "File contents"; // this uses operator string()
if (std::FILE* f = std::fopen(p.c_str(), "r"))
{
int ch;
while ((ch=fgetc(f))!= EOF) putchar(ch);
std::fclose(f);
}
// multibyte and wide representation can be used for output
std::cout.imbue(std::locale());
std::cout << "\nFile name in narrow multibyte encoding: "
<< p.string() << '\n';
std::wcerr.imbue(std::locale());
std::wcerr << "File name in wide encoding: "
<< p.wstring() << '\n';
fs::remove(p);
}