【发布时间】:2019-10-20 16:17:01
【问题描述】:
我有一个简单的程序,它将用户输入作为字符串并输出输入的字符串。唯一的区别是我为用户提供了两个选项,
首先输入一个基本字符串。
第二次输入一个宽字符串。
scanf() 成功接受用户输入的基本字符串,但 wscanf() 不提示用户输入并直接退出。
为什么这发生在 wscanf() 而不是 scanf() ?我将如何在同一程序中从 wscanf() 获取用户输入字符串。
#include <stddef.h>
#include <stdio.h>
#include <wchar.h>
void basic()
{
char str[100];
printf("Enter string with basic string char: ");
scanf("%s", str);
printf("Entered string : %s \n", str);
}
void wide()
{
wchar_t str[100];
wprintf(L"Enter string with wide string char: ");
wscanf(L"%ls", str);
wprintf(L"Entered string : %ls \n", str);
}
int main(int argc, char **argv)
{
int option = 1;
printf("\n Basic string (char*) vs Wide string (wchar_t*) \n\n");
printf("1. Basic string \n2. Wide string \n");
printf("Enter choice : ");
scanf("%d", &option);
switch(option)
{
case 1:
basic();
break;
case 2 :
wide();
break;
default:
printf("Invalid choice \n");
}
return 0;
}
输出:
1.基本字符串:
Basic string (char*) vs Wide string (wchar_t*)
1. Basic string
2. Wide string
Enter choice : 1
Enter string with basic string char: hello
Entered string : hello
2。宽字符串:
Basic string (char*) vs Wide string (wchar_t*)
1. Basic string
2. Wide string
Enter choice : 2
【问题讨论】:
-
查看wscanf的返回值。此外,最好提供您的编译器和系统的详细信息,因为宽字符串函数实现已经(并且仍然)有很多种类,通常不遵循标准
标签: c string scanf c-strings widestring