将 Unicode UTF-8 文件读入 wstring

Read Unicode UTF-8 file into wstring(将 Unicode UTF-8 文件读入 wstring)
本文介绍了将 Unicode UTF-8 文件读入 wstring的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

如何在 Windows 平台上将 Unicode (UTF-8) 文件读入 wstring(s)?

How can I read a Unicode (UTF-8) file into wstring(s) on the Windows platform?

推荐答案

在 C++11 支持下,您可以使用 std::codecvt_utf8 facet 封装了UTF-8编码的字节串与UCS2或UCS4字符串之间的转换可用于读写UTF-8文件, 文本和二进制.

With C++11 support, you can use std::codecvt_utf8 facet which encapsulates conversion between a UTF-8 encoded byte string and UCS2 or UCS4 character string and which can be used to read and write UTF-8 files, both text and binary.

为了使用facet,您通常会创建区域设置对象 将特定于文化的信息封装为一组共同定义特定本地化环境的方面.一旦您有了区域设置对象,您可以注入您的流缓冲区:

In order to use facet you usually create locale object that encapsulates culture-specific information as a set of facets that collectively define a specific localized environment. Once you have a locale object, you can imbue your stream buffer with it:

#include <sstream>
#include <fstream>
#include <codecvt>

std::wstring readFile(const char* filename)
{
    std::wifstream wif(filename);
    wif.imbue(std::locale(std::locale::empty(), new std::codecvt_utf8<wchar_t>));
    std::wstringstream wss;
    wss << wif.rdbuf();
    return wss.str();
}

可以这样使用:

std::wstring wstr = readFile("a.txt");

或者,您可以在使用字符串流之前设置全局 C++ 语言环境, 导致所有未来对 std::locale 默认构造函数的调用返回全局 C++ 语言环境的副本(然后您不需要显式地将其注入流缓冲区):

Alternatively you can set the global C++ locale before you work with string streams which causes all future calls to the std::locale default constructor to return a copy of the global C++ locale (you don't need to explicitly imbue stream buffers with it then):

std::locale::global(std::locale(std::locale::empty(), new std::codecvt_utf8<wchar_t>));

这篇关于将 Unicode UTF-8 文件读入 wstring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)
STL BigInt class implementation(STL BigInt 类实现)
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)