std::experi...stem::u8path(3)
Welcome to the std::experi...stem::u8path(3) page from the C++ Standard Libary manual β yes, that spelling is in the header and we are keeping it π
β¨
πΈ NAME πΈ
std::experimental::filesystem::u8path β std::experimental::filesystem::u8path. Thatβs the name, thatβs the function, periodt π
β¨ Synopsis β¨
Defined in header <experimental/filesystem>
template< class Source > (1) (filesystem TS)
path u8path( const Source& source );
template< class InputIt > (2) (filesystem TS)
path u8path( InputIt first, InputIt last );
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. Slay π¦
- If
path::value_typeischarand native encoding is UTF-8, constructs apathdirectly as if bypath(source)orpath(first, last). Note: this is the typical situation of a POSIX system that uses Unicode, such as Linux. - Otherwise, if
path::value_typeiswchar_tand native encoding is UTF-16 (this is the situation on Windows), or ifpath::value_typeischar16_t(native encoding guaranteed UTF-16) orchar32_t(native encoding guaranteed UTF-32), then first converts the UTF-8 character sequence to a temporary stringtmpof typepath::string_typeand then the newpathis constructed as if bypath(tmp). - Otherwise (for non-UTF-8 narrow character encodings and for non-UTF-16
wchar_t), first converts the UTF-8 character sequence to a temporary UTF-32-encoded stringtmpof typestd::u32string, and then the newpathis constructed as if bypath(tmp)(thispathis taken on a POSIX system with a non-Unicode multibyte or single-byte encoded filesystem).
π Parameters π
sourceβ a UTF-8 encodedstd::string, pointer to a null-terminated multibyte string, or an input iterator withcharvalue type that points to a null-terminated multibyte string.first,lastβ pair ofLegacyInputIteratorsthat specify a UTF-8 encoded character sequence. Bestie, thatβs the iterator duo π
π¦ Type requirements π¦
InputItmust meet the requirements ofLegacyInputIterator.- The value type of
InputItmust bechar.
π Return value π
The path constructed from the input string after conversion from UTF-8 to the filesystemβs native character encoding. Cute, converted, done π
π Exceptions π
May throw filesystem_error on underlying OS API errors or std::bad_alloc if memory allocation fails. So yeah, keep an eye out π
π§ 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. Thatβs the tea ββ¨
π§ͺ Example π§ͺ
// Run this code
#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);
}
πΈ Possible output πΈ
File contents
File name in narrow multibyte encoding: θ¦γγͺγ.txt
File name in wide encoding: θ¦γγͺγ.txt
π See also π
pathβ represents a path (class). Iconic adjacent behavior π
π·οΈ Category π·οΈ
- Noindexed pages
Source: http://cppreference.com Β· version/date: 2024.06.10 β and thatβs the std::experi...stem::u8path(3) page, mwah πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms