使用C scanf_s输入字符串

问题描述 投票:0回答:7

我自己一直在寻找答案,但找不到。 我想插入一部分编程,读取“Hello”之类的字符串并存储并可以在需要时显示它,以便

printf("%s", blah);
产生
Hello

这是给我带来麻烦的代码部分

char name[64];
scanf_s("%s", name);
printf("Your name is %s", name);

我知道

printf
不是问题;在提示后输入某些内容后,程序崩溃。请帮忙?

c string scanf c11 tr24731
7个回答
7
投票

来自ISO/IEC 9899:2011标准附录K.3.5.3.2中

fscanf_s()
的规范:

fscanf_s
函数与
fscanf
等效,只是
c
s
[
转换 说明符适用于一对参数(除非赋值抑制由
*
)。第一个参数与
fscanf
相同。这个论点是 参数列表中紧随其后的是第二个参数,其类型为
rsize_t
并给出第一个参数指向的数组中的元素数量 这对的。如果第一个参数指向标量对象,则将其视为数组 一个元素。

和:

scanf_s
函数相当于带有参数
fscanf_s
stdin
插入到
scanf_s
的参数之前。

MSDN 也说了类似的话(

scanf_s()
fscanf_s()
)。

您的代码不提供长度参数,因此使用了其他数字。它不确定它找到什么值,因此您会从代码中得到奇怪的行为。您需要更多类似这样的东西,其中换行符有助于确保实际看到输出。

char name[64];
if (scanf_s("%s", name, sizeof(name)) == 1)
    printf("Your name is %s\n", name);

5
投票

我在大学课程中经常使用这个,所以这在 Visual Studio 中应该可以正常工作(在 VS2013 中测试):

char name[64]; // the null-terminated string to be read
scanf_s("%63s", name, 64);
// 63 = the max number of symbols EXCLUDING '\0'
// 64 = the size of the string; you can also use _countof(name) instead of that number
// calling scanf_s() that way will read up to 63 symbols (even if you write more) from the console and it will automatically set name[63] = '\0'
// if the number of the actually read symbols is < 63 then '\0' will be stored in the next free position in the string
// Please note that unlike gets(), scanf() stops reading when it reaches ' ' (interval, spacebar key) not just newline terminator (the enter key)
// Also consider calling "fflush(stdin);" before the (eventual) next scanf()

参考:https://msdn.microsoft.com/en-us/library/w40768et.aspx


0
投票

这是对我来说很好用的代码的一部分:

char name[64];
scanf_s("%63s", name,(unsigned)_countof(name));
printf("Your name is %s", name);

欲了解更多信息,请点击以下链接: https://learn.microsoft.com/de-de/cpp/c-runtime-library/reference/scanf-s-scanf-s-l-wscanf-s-wscanf-s-l?view=msvc-170

致以诚挚的问候


0
投票

scanf_s
函数与
scanf
等效,不同之处在于
%c
%s
%[
转换说明符均需要两个参数(通常的指针和
rsize_t
类型的值,指示接收的大小)数组,当使用
%c
读取单个字符时可能为 1)

您的代码没有提供接收数组的大小,而且变量

name
是指向数组第一个字符的指针,因此它包含
name[0]
的地址。因此,
name
中的第一个参数
scanf_s
是正确的,因为 name 是一个指针,还要注意,对于第二个参数,您不能插入像
sizeof(name)
这样的指针的大小,因为它总是相同的。您需要指定字符数组的大小 (
name[64]
),因此对于第二个参数,您应该插入
sizeof(name)
sizeof(char[64])
64*sizeof(char)

您可以按如下方式更正您的代码:

char name[64];
if (scanf_s("%s", name, sizeof(name)) == 1)
    printf("Your name is %s\n", name);

-1
投票
    #include<stdio.h>

    int main()
    {
      char name[64];

      printf("Enter your name: ");
      scanf("%s", name);
      printf("Your name is %s\n", name);

     return 0;
    }

-1
投票
#include<stdio.h>

int main()
{
  char name[64];
  printf("Enter your name: ");
  gets(name);
  printf("Your name is %s\n", name);
 return 0;
}

-1
投票

你应该这样做:

scanf ("%63s", name);

更新

下面的代码对我有用:

#include <stdio.h> 
int main(void) { 
    char name[64]; 
    scanf ("%63s", name); 
    printf("Your name is %s", name); 
    return 0; 
}

如果您使用的是 Visual Studio, 转到

Project properties -> Configuration Properties -> C/C++-> Preprocessor -> Preprocessor Definitions
单击
edit
并添加
_CRT_SECURE_NO_WARNINGS
单击确定,应用设置并再次运行。

注意:只有当您正在做作业或类似的事情时,这才有用,不建议用于生产。

© www.soinside.com 2019 - 2024. All rights reserved.