2

std::wofstream用来在文本文件中写入字符。我的字符可以有来自非常不同语言的字符(英文到中文)。我想将我的打印vector<wstring>到该文件中。如果我的矢量只包含英文字符,我可以毫无问题地打印它们。但是如果我写中文字符,我的文件仍然是空的。

我浏览了stackoverflow,所有答案基本上都说使用库中的函数:

#include <codecvt>

我不能包含那个库,因为我在 5.11 版中使用的是 Dev-C++。我做到了:#define UNICODE在我所有的头文件中。我想这个问题有一个非常简单的解决方案。如果有人可以帮助我,那就太好了。

我的代码:

#define UNICODE
#include <string>
#include <fstream>

using namespace std;

int main()
{
    string Path = "D:\\Users\\\t\\Desktop\\korrigiert_RotCommon_zh_check_error.log";
    wofstream Out;
    wstring eng = L"hello";
    wstring chi = L"程序";

    Out.open(Path, ios::out);

    //works.
    Out << eng;

    //fails
    Out << chi;

    Out.close();

    return 0;
}

亲切的问候

4

2 回答 2

1

首先,事件的名称wofstream暗示它是一个宽字符流,它不是。它仍然是一个字符流。它使用来自语言环境的转换方面将 wchars 转换为 char。

这是 cppreference 所说的:

std::basic_fstream<CharT>通过使用std::codecvt<CharT, char, std::mbstate_t>流中包含的语言环境的方面执行的所有文件 I/O 操作。

因此,您可以将全局语言环境设置为支持中文或imbue流的语言环境。在机器人案例中,您将获得一个单字节流。

#include <locale>
//...
const std::locale loc = std::locale(std::locale(), new std::codecvt_utf8<wchar_t>);

Out.open(Path, ios::out);
Out.imbue(loc);

不幸std::codecvt_utf8的是已经被弃用[ 2 ]。这篇 MSDN 杂志文章解释了如何使用MultiByteToWideChar C++ 进行 UTF-8 转换 - Unicode 编码转换与 STL 字符串和 Win32 API

这里是转换的Microsoft/vcpkg变体to_utf8

std::string to_utf8(const CWStringView w)
{
    const size_t size = WideCharToMultiByte(CP_UTF8, 0, w.c_str(), -1, nullptr, 0, nullptr, nullptr);
    std::string output;
    output.resize(size - 1);
    WideCharToMultiByte(CP_UTF8, 0, w.c_str(), -1, output.data(), size - 1, nullptr, nullptr);
    return output;
 }

另一方面,您可以使用普通的二进制流并wstring使用write().

std::ofstream Out(Path, ios::out | ios::binary);

const uint16_t bom = 0xFEFF;
Out.write(reinterpret_cast<const char*>(&bom), sizeof(bom));    // optional Byte order mark

Out.write(reinterpret_cast<const char*>(chi.data()), chi.size() * sizeof(wchar_t));
于 2018-02-16T12:30:14.643 回答
-1

您忘记告诉您的流使用什么语言环境:

Out.imbue(std::locale("zh_CN.UTF-8"));

您显然需要为此包括在内<locale>

于 2018-02-16T12:17:23.457 回答