C++0x 中的 Unicode 支持

2022-01-07 00:00:00 unicode stream c++ c++11

我正在尝试在 C++0x 中使用新的 unicode 字符.所以我写了示例代码:

#include #include <字符串>int main(){std::u32string str = U"Hello World";std::basic_ofstreamfout("输出.txt");fout<

但是在执行这个程序后,我得到了空的 output.txt 文件.那么为什么不打印 Hello World?

还有已经为这些类型定义的coutcin,或者stdinstdout不支持 Unicode?

我使用的是 g++ 和 Linux.

АТТЕNTION.我发现,标准委员会驳回了 C++0x 中的 Unicode 流.所以以前接受的答案不再正确.有关详细信息,请参阅我的回答!

解决方案

Unicode 字符串文字支持 开始 在 GCC 4.5 中.也许这就是问题所在.

经过一些挖掘,我发现这个新的 unicode 文字的流在 N2035 它是 包括在标准草案中.根据此文档,您需要 u32ofstream 来输出字符串,但 GCC 4.5 C++0x 库中不存在此类.

作为一种解决方法,您可以使用普通的 fstream:

std::ofstream fout2("output2.txt", std::ios::out | std::ios::binary);fout2.write((const char *)str.c_str(), str.size() * 4);

这样我就在我的 Intel 机器上以 UTF-32LE 格式输出你的字符串(小端).

我对 u32ofstream 的状态有点错误:根据 最新草案关于 C++ 标准委员会的网站 你必须像你一样使用 std::basic_ofstream.此类将使用必须在标准库中实现的 codecvt<char32_t,char,typename traits::state_type> 类(参见 §27.9.1.1 末尾)(搜索 codecvt<char32_t 在文档中),但它在 GCC 4.5 中不可用.

I'm trying to use new unicode characters in C++0x. So I wrote sample code:

#include <fstream>
#include <string>
int main()
{
    std::u32string str = U"Hello World";

    std::basic_ofstream<char32_t> fout("output.txt");

    fout<<str;  
    return 0;
}

But after executing this program I'm getting empty output.txt file. So why it's not printing Hello World?

Also is there something like a cout and cin already defined for these types, or stdin and stdout doesn't support Unicode?

Edit: I'm using g++ and Linux.

EDIT:АТТЕNTION. I have discovered, that standard committee dismissed Unicode streams from C++0x. So previously accepted answer is not correct anymore. For more information see my answer!

解决方案

Unicode string literals support began in GCC 4.5. Maybe that's the problem.

[edit]

After some digging I've found that streams for this new unicode literals are described in N2035 and it was included in a draft of the standard. According to this document you need u32ofstream to output you string but this class is absent in GCC 4.5 C++0x library.

As a workaround you can use ordinary fstream:

std::ofstream fout2("output2.txt", std::ios::out | std::ios::binary);
fout2.write((const char *)str.c_str(), str.size() * 4);

This way I've output your string in UTF-32LE on my Intel machine (which is little-endian).

[edit]

I was a little bit wrong about the status of u32ofstream: according to the latest draft on the The C++ Standards Committee's web site you have to use std::basic_ofstream<char32_t> as you did. This class would use codecvt<char32_t,char,typename traits::state_type> class (see end of §27.9.1.1) which has to be implemented in the standard library (search codecvt<char32_t in the document), but it's not available in GCC 4.5.

相关文章