windows 使用 std::wofstream 写入文件。该文件仍然为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3950718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:25:21  来源:igfitidea点击:

Wrote to a file using std::wofstream. The file remained empty

c++windowsvisual-studioencodingwofstream

提问by Armen Tsirunyan

I wrote the following program using VS2008:

我使用 VS2008 编写了以下程序:

#include <fstream>
int main()
{
    std::wofstream fout("myfile");
    fout << L"???????? Россия ?sterreich Ελλ?δα ????" << std::endl;
}

When I tried to compile it the IDE asked me whether I wanted to save my source file in unicode, I said "yes, please".
Then I run the program, and myfile appeared in my project's folder. I opened it with notepad, the file was empty. I recalled that notepad supported only ASCII data. I opened it with WordPad, it was still empty. Finally the little genius inside me urged me to look at the file size and not surprisingly it was 0 bytes. So I rebuilt and reran the program, to no effect. Finally I decided to ask very intelligent people on StackOverflow as to what I am missing and here I am :)

当我试图编译它时,IDE 询问我是否想以 unicode 保存我的源文件,我说“是的,请”。
然后我运行程序,myfile 出现在我的项目文件夹中。我用记事本打开它,文件是空的。我记得记事本只支持 ASCII 数据。我用写字板打开它,它仍然是空的。最后,我内心的小天才催促我查看文件大小,毫不奇怪,它是 0 字节。所以我重建并重新运行程序,没有任何效果。最后,我决定在 StackOverflow 上询问非常聪明的人我错过了什么,我在这里:)

Edited:

编辑:

After the abovementioned intelligent people left some comments, I decided to follow their advice and rewrote the program like this:

在上述聪明人留下了一些评论后,我决定听从他们的建议,将程序改写成这样:

#include <fstream>
#include <iostream>
int main()
{
    std::wofstream fout("myfile");
    if(!fout.is_open())
    {
        std::cout << "Before: Not open...\n";
    }
    fout << L"???????? Россия ?sterreich Ελλ?δα ????" << std::endl;
    if(!fout.good())
    {
        std::cout << "After: Not good...\n";
    }
}

Built it. Ran it. And... the console clearly read, to my surprise: "After: Not good...". So I edited my post to provide the new information and started waiting for answers which would explain why this is and what I could do. :)

建造了它。跑了 而且……控制台清楚地读到,令我惊讶的是:“之后:不好……”。所以我编辑了我的帖子以提供新信息并开始等待答案,这些答案可以解释为什么会这样以及我可以做什么。:)

采纳答案by Artyom

In Visual studio the output stream is always written in ANSI encoding, and it does not support UTF-8 output.

在 Visual Studio 中,输出流始终以 ANSI 编码编写,并且不支持 UTF-8 输出。

What is basically need to do is to create a locale class, install into it UTF-8 facet and then imbue it to the fstream.

基本上需要做的是创建一个语言环境类,将 UTF-8 facet 安装到其中,然后将其注入 fstream。

What happens that code points are not being converted to UTF encoding. So basically this would not work under MSVC as it does not support UTF-8.

代码点未转换为 UTF 编码会发生什么情况。所以基本上这在 MSVC 下不起作用,因为它不支持 UTF-8。

This would work under Linux with UTF-8 locale

这将在使用 UTF-8 语言环境的 Linux 下工作

#include <fstream>
int main()
{
    std::locale::global(std::locale(""));
    std::wofstream fout("myfile");
    fout << L"???????? Россия ?sterreich Ελλ?δα ????" << std::endl;
}

~ And under windows this would work:

〜在Windows下这将起作用:

#include <fstream>
int main()
{
    std::locale::global(std::locale("Russian_Russia"));
    std::wofstream fout("myfile");
    fout << L"Россия" << std::endl;
}

As only ANSI encodings are supported by MSVC.

因为 MSVC 只支持 ANSI 编码。

Codecvt facet can be found in some Boost libraries. For example: http://www.boost.org/doc/libs/1_38_0/libs/serialization/doc/codecvt.html

Codecvt facet 可以在一些 Boost 库中找到。例如:http: //www.boost.org/doc/libs/1_38_0/libs/serialization/doc/codecvt.html

回答by DerKuchen

MSVC offers the codecvt_utf8locale facet for this problem.

MSVCcodecvt_utf8为这个问题提供了语言环境方面。

#include <codecvt>

// ...  
std::wofstream fout(fileName);
std::locale loc(std::locale::classic(), new std::codecvt_utf8<wchar_t>);
fout.imbue(loc);