首页 > 其他 > 详细

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

时间:2018-12-31 20:04:27      阅读:181      评论:0      收藏:0      [点我收藏+]

在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;
}

 

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

原文:https://www.cnblogs.com/buyishi/p/10203135.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!