首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::filesystem::u8path

Defined in header <filesystem>

template< class Source > path u8path( const Source& source );

(1)

(since C++17)

template< class InputIt > path u8path( InputIt first, InputIt last );

(2)

(since C++17)

构造路径p的UTF-8编码序列charS,作为std::string,或std::string_view,或作为以空结尾的多字节字符串,或作为。[首先,最后%29迭代器对。

  • 如果path::value_typechar而本机编码是utf-8,直接构造路径,就像path(source)path(first, last)注意:这是使用Unicode(如Linux)的POSIX系统的典型情况。
  • 否则,如果path::value_typewchar_t本机编码是utf-16%28--这是Windows%29上的情况,或者如果path::value_typechar16_t%28本机编码保证utf-16%29或char32_t%28本机编码保证utf-32%29,然后首先将utf-8字符序列转换为临时字符串。tmp类型path::string_type然后新的路径被构造成path(tmp)
  • 否则,对于非UTF-8窄字符编码和非UTF-16 wchar,则为%28。[医]t%29,首先将utf-8字符序列转换为临时的utf-32编码字符串。tmp类型std::u32string,然后新路径被构造为path(tmp)%28此路径是在具有非Unicode多字节或单字节编码的文件系统%29的POSIX系统上采取的。

参数

source

-

a UTF-8 encoded std::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 of InputIterators that specify a UTF-8 encoded character sequence

类型要求

-输入必须符合输入器的要求。

-输入的值类型必须是字符

返回值

从UTF-8转换到文件系统%27 s本机字符编码后从输入字符串构造的路径。

例外

可抛filesystem_error关于基础OS API错误或std::bad_alloc如果内存分配失败。

注记

在本机路径格式与泛型路径格式%28不同的系统上,Windows或POSIX系统都不是这类OSes%29的示例,如果此函数的参数使用泛型格式,则将转换为本机。

二次

代码语言:javascript
复制
#include <cstdio>
#ifdef _MSC_VER
#include <io.h>
#include <fcntl.h>
#else
#include <locale>
#include <clocale>
#endif
#include <fstream>
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
 
int main()
{
#ifdef _MSC_VER
    _setmode(_fileno(stderr), _O_WTEXT);
#else
    std::setlocale(LC_ALL, "");
    std::locale::global(std::locale(""));
    std::cout.imbue(std::locale());
    std::wcerr.imbue(std::locale());
#endif
 
    fs::path p = fs::u8path(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 APIs
    if (std::FILE* f =
#ifdef _MSC_VER
                _wfopen(p.c_str(), L"r")
#else
                std::fopen(p.c_str(), "r")
#endif
        )
    {
        int ch;
        while((ch=fgetc(f))!= EOF) putchar(ch);
        std::fclose(f);
    }
 
    // multibyte and wide representation can be used for output
    std::cout << "\nFile name in narrow multibyte encoding: " << p.string() << '\n';
    std::wcerr << "File name in wide encoding: " << p.wstring() << '\n';
 
    fs::remove(p);
}

二次

产出:

二次

代码语言:javascript
复制
File contents
File name in narrow multibyte encoding: 要らない.txt
File name in wide encoding: 要らない.txt

二次

另见

path (C++17)

represents a path (class)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券