freopen和fwprintf将不正确的宽字符写入TXT文件

时间:2022-01-18 00:05:09

I've already spent the whole day searching for an answer about UTF-8 and UTF-16 options when freopen and fwprintf used and no results for now. I will add my code below, maybe someone can help. Thanks in advance.

当freopen和fwprintf使用时,我已经花了一整天时间寻找关于UTF-8和UTF-16选项的答案,现在没有结果。我将在下面添加我的代码,也许有人可以提供帮助。提前致谢。

template<typename... ArgsT>
void log(const wchar_t* message, ArgsT... args)
{
    fwprintf(stdout, message, args...);
    fwprintf(stdout, L"\n");
    fflush(stdout);
}

int main()
{
    bool init = true;
    if (!std::freopen("log.txt", "w", stdout))
    {
        init = false;
    }

    if (std::fwide(stdout, 1) <= 0)
    {
        init = false;
    }

    if (init)
    {
        std::wstring str = L"кирилиця";
        log(L"Some text in cyrillic %S and some number %i", str.c_str(), 10);
    }

    return 0;
}

As the result in TXT file I have: Some text in cyrillic :8@8;8FO and some number 10

作为TXT文件的结果我有:一些西里尔文:8 @ 8; 8FO和10

1 个解决方案

#1


0  

You need to start your file with wchar_t(0xFEFF). It tells text editor apps to treat following data as unicode.

您需要使用wchar_t(0xFEFF)启动文件。它告诉文本编辑器应用程序将以下数据视为unicode。

#1


0  

You need to start your file with wchar_t(0xFEFF). It tells text editor apps to treat following data as unicode.

您需要使用wchar_t(0xFEFF)启动文件。它告诉文本编辑器应用程序将以下数据视为unicode。