宽字符,爱你不容易

    技术2022-05-11  143

    看了 宽字符的输出 一文,也想试用下stlport5.1 + mingw 的宽字符。 以前也试过,不过在mingw上好象并不支持wcout,libstd++不支持,stlport 较早版本也不支持。 现在stlport5.1终于让mingw有了宽字符了! 写个简单的代码: #include <iostream> using namespace std; int main() {     wcout.imbue(locale(""));     wcout << L"你好" << endl;         return 0; } 需要注意: 1. 源文件中有中文,我在vi中不能输入中文,所以是用Windows编辑工具写的。 这里有个编码问题,一般不指定就会自动保存为GB2312编码。 gcc将报错:converting to execution character set: Illegal byte sequence 因为gcc是按UTF-8编码读入源文件并输出可执行程序的。 使用 -finput-charset=GB2312 会产生一堆错误: 根源是: In file included from .../stlport/stl/char_traits.h:43,                  from .../stlport/stl/_iosfwd.h:22,                  from .../stlport/iostream:38,                  from main.cpp:2: .../stlport/stl/_construct.h:46:39: failure to convert GB2312 to UTF-8 按理所包含的stlport头文件是纯ASCII,可以当GB2312处理,此处不明! 反正 -finput-charset=GB2312 不能解决问题。 将源文件保存为UTF-8就好了。我用notepad保存,再用UEdit去除头部3 个字节。 2. 必须显式指定local:    wcout.imbue(locale("")); 或者更明确地    wcout.imbue(locale("chs"));不然输出中文只是个“?”,或者乱码。可能程序缺省按UTF-8输出了,但是编译时设定 -fexec-charset=GB2312也没有用。

    最新回复(0)