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

此方法也可用于C++中

例:

#include <stdio.h>
#include <locale.h>
int main()
{
    setlocale(LC_ALL, "");
    const char *str = "中文\n";
    printf(str);
    const wchar_t *wstr = L"中文\n";
    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;
}

 

相关文章:

  • 2021-12-18
  • 2022-12-23
  • 2021-12-29
  • 2021-05-26
  • 2021-12-01
  • 2021-07-19
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-07-11
  • 2022-02-10
  • 2022-12-23
  • 2022-12-23
  • 2021-11-13
  • 2022-12-23
相关资源
相似解决方案