wprintf、wcout无法输出中文的解决方案

在C语言中,若wprintf无法输出中文,调用函数setlocale(int category, const char *locale)设置locale即可输出中文

此方法也可用于C++中

例:

#include <stdio.h>
#include <locale.h>
int main()
{
    setlocale(LC_ALL, "");
    const char *str = "中文
";
    printf(str);
    const wchar_t *wstr = L"中文
";
    wprintf(wstr);
    system("pause");
    return 0;
}

在C++中,若wcout无法输出中文,调用函数wcout.imbue(const locale &loc)替换当前locale即可输出中文

例:

#include <iostream>
#include <string>
int main()
{
    using namespace std;
    string str = "英文";
    cout << str << endl;
    wcout.imbue(locale("chs"));
    wstring wstr = L"英文";
    wcout << wstr << endl;
    system("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/buyishi/p/10203135.html