读/写unicode c ++ [英] Read/write unicode c++

查看:153
本文介绍了读/写unicode c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很遗憾,这是本周第三次发布问题。



我必须用unicode编码(或UTF8)将文本写入文件。


这是我所做的:



创建 wofstream mystream; 然后我把一个 wstring 在它像这样 mystream< Lhello world;



第一个问题:流在我的案例中使用什么样的编码?



其次,我想加载我的新文件,但如何读取行? ifstream getline 无法正常工作,因为行最后明显损坏。


wstream 的类型 和 wstring 是平台相关的:在Windows上为2个字节,在某些(所有?)Linux上为4个字节。所以你最终会写'Unicode',但确切地说,Unicode是受许多变量。你可以写UTF32 / UCS4,你最后可能会得到UTF16 / UCS2。



如果要使用特定的,受控的编码(例如,UTF8或UCS-2LE与UCS-2BE来控制字节序)编写, iconv 。您还可以使用 std :: locale imbue 流,请参阅 http:// stackoverflow。 com / a / 1275260/105929


Sadly this is the third time this week I have to post a question.

I have to write text to a file with unicode encoding (or UTF8).
This is what I do:

creating wofstream mystream; and then I put a wstring in it like this mystream << L"hello world";

First question: what kind of encoding the stream uses in my case?

Secondly, I want to load my new file, but how to read the lines? The ifstream's getline is not working because the line ends up ruined obviously.

解决方案

wchar_t, the type that backs wstream and wstring, is platform dependent: 2 bytes on Windows, 4 bytes on some (all?) Linux. So you will end up writing 'Unicode', but exactly which Unicode is subject to many variables. You may write UTF32/UCS4, you may end up with UTF16/UCS2.

If you want to write using a specific, well controlled encoding (eg. UTF8, or UCS-2LE vs. UCS-2BE to control endianess) then you need something like iconv. You can also use std::locale to imbue a stream, see http://stackoverflow.com/a/1275260/105929.

这篇关于读/写unicode c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆