wprintf、wcout無法輸出中文的解決方案
阿新 • • 發佈:2018-12-31
pau clas category ret str out har stdio.h use
在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無法輸出中文的解決方案