wprintf()什么都没有输出?

Sidneylee 2006-04-21 01:21:49
main()
{
wchar_t *str=L"我是一只来自北方的狼";
char *str1="abcdef";


printf("%s\n",str1);
wprintf(L"%s\n",str);
}
...全文
1466 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xyxfly 2007-03-24
  • 打赏
  • 举报
回复 1
#include <stdio.h>
#include <locale.h>

int main()
{
wchar_t *str = L"我是一只来自北方的狼";
char *str1 = "abcdef";

setlocale(LC_ALL, ".936");

printf("%s\n",str1);

wprintf(L"%s\n",str);
}
jhb92 2007-03-23
  • 打赏
  • 举报
回复 1
1. printf 只能提供ANSI/MB 的输出,不支持输出unicode stream.
例如:
wchar_t test[]=L"测试1234";
printf("%s",test);是不会正确输出的


2.wprintf 同样不会提供unicode output,
但是他会把wchar_t的string转为locale的SB/MB字符编码,然后输出
例如:
wchar_t test[] = L"测试Test";
wprintf(L"%s",test);会输出??1234之类的字符串,或者不输出任何结果
因为wprintf没有办法把L"测试Test"转为默认的ANSI,需要设置locale
setlocale(LC_ALL,"chs");
wchar_t test[] = L"测试Test";
wprintf(L"%s",test);会有正确的输出
等同于printf("%ls",test);

综上: CRT I/O functions do not provide Unicode output.

3. Window console自从NT4就是一个真正的unicode console
不过输出unicode string,只有使用Windows API, WriteConsoleW
例如:
wchar_t test[] = L"测试1234";
DWORD ws;
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE),test,wcslen(test),&ws,NULL);可以正确的输出而不需要设置locale,因为是真正的unicode的输出,跟codepage无关
woshihuzi 2007-03-23
  • 打赏
  • 举报
回复
#include <locale.h>

...

setlocale(LC_ALL,"chinese");
henbing 2006-11-25
  • 打赏
  • 举报
回复

wprintf(L"%s\n",str); 好象好似把字符放到前面的字符里面
Stefine 2006-11-25
  • 打赏
  • 举报
回复
确实不能输出中文

原因?

这里有相关帖子:
http://tech.eyeah.cn/3117/1114/1139/286659744.html
http://bbs.chinaunix.net/viewthread.php?tid=270513
LiChenYue 2006-11-24
  • 打赏
  • 举报
回复
不会吧?
ahuo 2006-11-24
  • 打赏
  • 举报
回复
没错
一条晚起的虫 2006-04-21
  • 打赏
  • 举报
回复
wprintf()不能输出中文?
sycnick 2006-04-21
  • 打赏
  • 举报
回复
Example
// crt_printf.c
/* This program uses the printf and wprintf functions
* to produce formatted output.
*/

#include <stdio.h>

int main( void )
{
char ch = 'h', *string = "computer";
int count = -9234;
double fp = 251.7366;
wchar_t wch = L'w', *wstring = L"Unicode";

/* Display integers. */
printf( "Integer formats:\n"
" Decimal: %d Justified: %.6d Unsigned: %u\n",
count, count, count, count );

printf( "Decimal %d as:\n Hex: %Xh C hex: 0x%x Octal: %o\n",
count, count, count, count );

/* Display in different radixes. */
printf( "Digits 10 equal:\n Hex: %i Octal: %i Decimal: %i\n",
0x10, 010, 10 );

/* Display characters. */

printf("Characters in field (1):\n%10c%5hc%5C%5lc\n", ch, ch, wch, wch);
wprintf(L"Characters in field (2):\n%10C%5hc%5c%5lc\n", ch, ch, wch, wch);

/* Display strings. */

printf("Strings in field (1):\n%25s\n%25.4hs\n %S%25.3ls\n",
string, string, wstring, wstring);
wprintf(L"Strings in field (2):\n%25S\n%25.4hs\n %s%25.3ls\n",
string, string, wstring, wstring);

/* Display real numbers. */
printf( "Real numbers:\n %f %.2f %e %E\n", fp, fp, fp, fp );

/* Display pointer. */
printf( "\nAddress as: %p\n", &count);

/* Count characters printed. */
printf( "\nDisplay to here:\n" );
printf( "1234567890123456%n78901234567890\n", &count );
printf( " Number displayed: %d\n\n", count );
}
Output
Integer formats:
Decimal: -9234 Justified: -009234 Unsigned: 4294958062
Decimal -9234 as:
Hex: FFFFDBEEh C hex: 0xffffdbee Octal: 37777755756
Digits 10 equal:
Hex: 16 Octal: 8 Decimal: 10
Characters in field (1):
h h w w
Characters in field (2):
h h w w
Strings in field (1):
computer
comp
Unicode Uni
Strings in field (2):
computer
comp
Unicode Uni
Real numbers:
251.736600 251.74 2.517366e+002 2.517366E+002

Address as: 0012FEE0

Display to here:
123456789012345678901234567890
Number displayed: 16

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧