繁体   English   中英

使用 C scanf_s 的字符串输入

[英]String input using C scanf_s

我一直试图自己寻找答案,但找不到。 我想插入程序的一部分,读取像“Hello”这样的字符串并存储并可以在我想要的时候显示它,这样printf("%s", blah); 产生Hello

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

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

我知道printf不是问题; 在提示后输入内容后程序崩溃。 请帮忙?

来自 ISO/IEC 9899:2011 标准附录 K.3.5.3.2 中fscanf_s()规范:

fscanf_s函数与fscanf等效,除了cs[转换说明符适用于一对参数(除非赋值抑制由*指示)。 这些参数中的第一个与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);

我在大学课程中经常使用它,所以这在 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

scanf_s function 等同于scanf ,除了%c%s%[转换说明符每个都需要两个 arguments (通常的指针和一个rsize_t类型的值,指示接收数组的大小,当使用 a 读取时可能为 1 %c成一个字符)

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

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

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

这是对我有用的代码的一部分:

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-sl-wscanf-s-wscanf-sl?view= msvc-170

最好的祝福

你应该这样做: 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单击确定,应用设置并再次运行。

注意:这仅在您正在做功课或类似的事情时才有用,并且不建议用于生产。

    #include<stdio.h>

    int main()
    {
      char name[64];

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

     return 0;
    }
#include<stdio.h>

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM