C ++文件字符编码 [英] C++ File character encoding
问题描述
好,所以我想使用C ++(Visual Studio 2012 Express)在W8下读取带有重音(法语)的json格式的文本文件.
Ok so I'm trying to read a json formatted text file with accents (French), under W8, using C++ (Visual Studio 2012 Express).
这是文件:
{"products": [{"id": 125, "label": "Billél"}, {"id": 4, "label": "Rùbin"}]}
一行以UTF-8编码(无BOM),另存为D:/p.txt
One line, encoded in UTF-8 (no BOM), saved as D:/p.txt
这是C ++中的阅读代码:
This is the reading code in C++:
std::ifstream in("D:/p.txt", std::ios::binary | std::ios::in);
std::string content( (std::istreambuf_iterator<char>(in) ), (std::istreambuf_iterator<char>() ) );
我得到的输出:
{"products": [{"id": 125, "label": "Bill├®l"}, {"id": 4, "label": "R├╣bin"}]}
尝试使用 CharToOemA :
{"products": [{"id": 125, "label": "Billél"}, {"id": 4, "label": "Rùbin"}]}
我的代码页应允许我在控制台中显示重音符号(我尝试回显这些重音符号,从而产生了完美的显示效果).我的c ++控制台的输入和输出代码页均为CP850(IBM Internatinal Latin-1).
My codepage should allow me to display accents in the console (I tried echoing such accents which yielded a perfectly good display). Both the input and output codepages for my c++ console is CP850 (IBM Internatinal Latin-1).
如何获取我的代码以在控制台中输出正确的口音?如果可能的话,我最终将需要一个跨平台的解决方案.
How can I get my code to output a correct accent in the console? I would ultimately need a cross-platform solution if possible.
推荐答案
您是否尝试过使用chcp 65001
将代码页切换为UTF-8,
Have you tried using chcp 65001
which should switch the code page to UTF-8, according to MSDN. Also note that the default console font might not be able to display all UTF8 glyphs, I recommend using Lucida Console
instead.
这篇关于C ++文件字符编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!