-2

我有一个文本文件,其中包含一些字符,包括一些 unicode 字符。我尝试使用以下编码保存此文件但未解决问题:UTF8、不带 BOM 的 UTF8、UCS2 BE 和 UCS2 LE。这是尝试逐行读取文件的代码。

    std::wifstream infile("my_file.txt");
    wchar_t line[1024];
    while (infile.getline(line, sizeof(line))) { ... }

"line" 有一个垃圾值代替 unicode 和 normal 那里有一个 ASCII 字符。它的值看起来像: L"Normal text here" 而不是 L"€Normal text here"

我尝试了其他变体,例如:

    std::wifstream infile("my_file.txt");
    std::wstring line;
    while (std::getline(infile, line)){ ... }

我也尝试过设置语言环境。我在 Windows 电脑上。如何让 unicode 按需要工作?我更喜欢一种适用于所有平台的格式,但在这个阶段我会采取任何措施。

谢谢。

4

2 回答 2

2

看来您需要在输入流中注入语言环境:请参阅https://stackoverflow.com/a/1275260/1967396

typedef wchar_t ucs4_t;

std::locale old_locale;
std::locale utf8_locale(old_locale,new utf8_codecvt_facet<ucs4_t>);

...

std::wifstream input_file("data.utf8");
input_file.imbue(utf8_locale);
ucs4_t item = 0;
while (ifs >> item) { ... }
于 2013-11-02T01:21:40.453 回答
1
于 2013-11-02T03:41:29.183 回答