如何在C ++中读取西里尔Unicode文件? [英] How to read Cyrillic Unicode file in C++?
本文介绍了如何在C ++中读取西里尔Unicode文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从已保存为Unicode的.txt文件中读取行. 这就是我的做法:
I'm trying to read lines from .txt files, that have been saved as Unicode. That's how i'm doing it:
wifstream input;
string path = "test.txt";
input.imbue(locale(input.getloc(),
new codecvt_utf16<wchar_t, 0x10ffff, consume_header>));
input.open(path);
if (input.is_open())
{
wstring line;
input.seekg( 1 , ios_base::beg);
getline(input, line);
}
它对于带有拉丁字符的文件很好用. 但是对于西里尔字母文件,我得到的是奇怪的符号,而不是空格和相邻字符.
It works fine for files with Latin characters. But for Cyrillic files I get weird symbols instead of spaces and adjacent characters.
例如:
输入文件中的内容:
ГосдепСШАосудилнападениена
Госдеп США осудил нападение на
我得到的是
︓осдепР!ШАР>судилР=ападениеР=а
︓осдепР!ШАР>судилР=ападениеР=а
我在做什么错了?
推荐答案
好了,想出了办法:
FILE *input= _wfopen(L"test.txt", L"rb");
wchar_t line[1000];
test.txtfgetws(line, 1000, input);
那样很好.我很愚蠢,不先尝试. 所以谢谢大家.
Works fine like that. Was quite stupid of me not to try it first. So thanks everyone.
这篇关于如何在C ++中读取西里尔Unicode文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文