64,642
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
#include <stdio.h>
#include <stdarg.h>
#include <iostream>
void printChinese(int x = 0)
{
va_list va;
va_start(va, x);
std::cout.clear();
printf("-----------\n");
vfwprintf_s(stdout, L"Print函数\n", va);
std::cout << "Cout流" << std::endl;
}
int main()
{
setlocale(LC_CTYPE, "C");
printChinese();
setlocale(LC_CTYPE, "");
printChinese();
setlocale(LC_CTYPE, ".936");
printChinese();
setlocale(LC_CTYPE, "chs");
printChinese();
std::cout.imbue(std::locale::classic());
printChinese();
std::cout.imbue(std::locale::locale("C"));
printChinese();
return 0;
}
-----------
PrintCout流
-----------
Print函数
Cout-----------
Print函数
Cout-----------
Print函数
Cout-----------
Print函数
Cout-----------
Print函数
Cout
#include "stdafx.h"
#include <stdio.h>
#include <stdarg.h>
#include <iostream>
#include <locale.h>
void printChinese(int x = 0)
{
va_list va;
va_start(va, x);
std::cout.clear();
printf("-----------\n");
_locale_t chs_local = _create_locale(LC_CTYPE, ".936");
_vfwprintf_p_l(stdout, L"Print函数\n", chs_local, va);
_free_locale(chs_local);
std::cout << "Cout流" << std::endl;
}
int main()
{
setlocale(LC_CTYPE, "C");
printChinese();
return 0;
}
-----------
PrintCout流
#pragma warning(disable:4786)
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main () {
wstring ws1,ws2;
wcin.imbue(locale("chs"));
wcout.imbue(locale("chs"));
getline(wcin,ws1);
getline(wcin,ws2);
if (ws1.size()!=ws2.size()) {
wcout<<ws1<<L" 和 "<<ws2<<L" 不对应"<<endl;
return 1;
}
map<wchar_t,wchar_t> m1,m2;
int n=ws1.size();
for (int i=0;i<n;i++) {
if (m1.find(ws1[i])==m1.end() && m2.find(ws2[i])==m2.end()) {
m1[ws1[i]]=ws2[i];
m2[ws2[i]]=ws1[i];
} else {
if (m1[ws1[i]]!=ws2[i] || m2[ws2[i]]!=ws1[i]) {
wcout<<ws1<<L" 和 "<<ws2<<L" 不对应"<<endl;
return 1;
}
}
}
wcout<<ws1<<L" 和 "<<ws2<<L" 对应"<<endl;
return 0;
}
setlocale(LC_CTYPE, "C");
printChinese();
// 你的输出Print,但输出不了函数这两个字,我这边测试也是这样,因为"C"不支持wchar*的中文输出
// 你的输出Cout流,我这边测试也是这样,"C"下面输出char*的中文,是没有问题的
setlocale(LC_CTYPE, "");
printChinese();
// 你的 输出Print函数,我这边测试也是这样,因为中文环境下,用w某某某这一系列的函数,输出wchar*的中文,是没有问题的
// 你的输出Cout,但输出不了流这个汉字,我这边测试不是这样,我这是能输出流这个汉字,中文环境下,用cout,输出char*的中文,是没有问题的
setlocale(LC_CTYPE, ".936");
printChinese();
// 同 setlocale(LC_CTYPE, "");
setlocale(LC_CTYPE, "chs");
printChinese();
// 同 setlocale(LC_CTYPE, "");
std::cout.imbue(std::locale::classic());
printChinese();
// 你的 输出Print函数,我这边测试也是这样
// 但是这里有个问题,如果没有前面的代码,只是单纯的设置std::cout.imbue(std::locale::classic());,w某某某的系列函数,是输出不了wchar*的中文的
// 只是由于你给的例子代码,在std::cout.imbue(std::locale::classic());之前, 还设置了setlocale(LC_CTYPE, "chs");
// 猜测如果先调用过了setlocale,再调用imbue是没有效果的,或者是有冲突的,没有研究过具体的代码,只是根据我这的运行现象做的猜测
// 所以你的代码只是凑巧,在在std::cout.imbue(std::locale::classic());之后,w某某某的系列函数还能正确的输出wchar*的中文的
// 你的输出Cout,但输出不了流这个汉字,我这边测试不是这样,我这是能输出流这个汉字的
// 这里和凑不凑巧就没有关系了,因为不管是设置"C",还是"chs",cout应该都是能输出char*的中文的
std::cout.imbue(std::locale::locale("C"));
printChinese();
// 同std::cout.imbue(std::locale::classic());
// Get the current locale. If we're in the C locale and the file is open
// in ANSI mode, we don't need double translation:
__acrt_ptd* const ptd = __acrt_getptd();
bool const is_c_locale = ptd->_locale_info->locale_name[LC_CTYPE] == nullptr;
if (is_c_locale && _textmode(fh) == __crt_lowio_text_mode::ansi)
return false;
对此有何解决办法没?
#include <stdio.h>
#include <stdarg.h>
#include <iostream>
void printChineseCpp(int x = 0, ...)
{
va_list va;
va_start(va, &x);
#ifdef UNICODE
wchar_t *p = va_arg(va, wchar_t *);
std::wcout << L"-----------" << std::endl;
std::wcout << L"Cout流," << p << std::endl;
#else
char *p = va_arg(va, char *);
std::cout << "-----------" << std::endl;
std::cout << "Cout流," << p << std::endl;
#endif
va_end(va);
}
void printChineseC(int x = 0, ...)
{
va_list va;
va_start(va, &x);
#ifdef UNICODE
wprintf(L"-----------\n");
vfwprintf_s(stdout, L"Print函数,%ls\n", va);
#else
printf("-----------\n");
vfprintf_s(stdout, "Print函数,%s\n", va);
#endif
va_end(va);
}
int main(int argc, char* argv[])
{
#ifdef UNICODE
std::wstring curLocale = _wsetlocale(LC_CTYPE, NULL);
_wsetlocale(LC_CTYPE, L"C");
printChineseC(1, L"一");
_wsetlocale(LC_CTYPE, L"");
printChineseC(2, L"二");
_wsetlocale(LC_CTYPE, L".936");
printChineseC(3, L"三");
_wsetlocale(LC_CTYPE, L"chs");
printChineseC(4, L"四");
_wsetlocale(LC_CTYPE, curLocale.c_str());
std::wcout.clear();
std::wcout.imbue(std::locale("C"));
printChineseCpp(5, L"五");
std::wcout.clear();
std::wcout.imbue(std::locale(""));
printChineseCpp(6, L"六");
std::wcout.clear();
std::wcout.imbue(std::locale(".936"));
printChineseCpp(7, L"七");
std::wcout.clear();
std::wcout.imbue(std::locale("chs"));
printChineseCpp(8, L"八");
#else
std::string curLocale = setlocale(LC_CTYPE, NULL);
setlocale(LC_CTYPE, "C");
printChineseC(1, "一");
setlocale(LC_CTYPE, "");
printChineseC(2, "二");
setlocale(LC_CTYPE, ".936");
printChineseC(3, "三");
setlocale(LC_CTYPE, "chs");
printChineseC(4, "四");
setlocale(LC_CTYPE, curLocale.c_str());
std::cout.clear();
std::cout.imbue(std::locale("C"));
printChineseCpp(5, "五");
std::cout.clear();
std::cout.imbue(std::locale(""));
printChineseCpp(6, "六");
std::cout.clear();
std::cout.imbue(std::locale(".936"));
printChineseCpp(7, "七");
std::cout.clear();
std::cout.imbue(std::locale("chs"));
printChineseCpp(8, "八");
#endif
return 0;
}
使用UNICODE字符集,则输出:(其中两个设置为“C”的,输出为乱码)
-----------
Print-----------
Print函数,二
-----------
Print函数,三
-----------
Print函数,四
-----------
Cout-----------
Cout流,六
-----------
Cout流,七
-----------
Cout流,八
使用多字节字符集,则输出:
-----------
Print函数,一
-----------
Print函数,二
-----------
Print函数,三
-----------
Print函数,四
-----------
Cout流,五
-----------
Cout流,六
-----------
Cout流,七
-----------
Cout流,八