C语言 使用 C scanf_s 输入字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23378636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:03:18  来源:igfitidea点击:

String input using C scanf_s

cstringscanfc11tr24731

提问by user3587529

I've been trying to look for answer myself, but I can't find one. I want to insert a part of the programming that reads in a string like "Hello" and stores and can display it when I want, so that printf("%s", blah);produces Hello.

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

Here's the code part that's giving me trouble

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

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

I know that printfisn't the problem; the program crashes after something is input after a prompt. Please help?

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

回答by Jonathan Leffler

From the specification of fscanf_s()in Annex K.3.5.3.2 of the ISO/IEC 9899:2011 standard:

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

The fscanf_sfunction is equivalent to fscanfexcept that the c, s, and [conversion specifiers apply to a pair of arguments (unless assignment suppression is indicated by a *). The first of these arguments is the same as for fscanf. That argument is immediately followed in the argument list by the second argument, which has type rsize_tand gives the number of elements in the array pointed to by the first argument of the pair. If the first argument points to a scalar object, it is considered to be an array of one element.

fscanf_s功能相当于fscanf不同之处在于cs[转换说明适用于对参数(除非分配抑制由指示 *)。这些参数中的第一个与 for 相同fscanf。该参数在参数列表中紧跟在第二个参数之后,该参数具有类型 rsize_t并给出由该对的第一个参数指向的数组中的元素数。如果第一个参数指向一个标量对象,它被认为是一个包含一个元素的数组。

and:

和:

The scanf_sfunction is equivalent to fscanf_swith the argument stdininterposed before the arguments to scanf_s.

scanf_s函数等效于fscanf_s在 的参数stdin之前插入参数scanf_s

MSDN says similar things (scanf_s()and fscanf_s()).

MSDN 说类似的话(scanf_s()fscanf_s())。

Your code doesn't provide the length argument, so some other number is used. It isn't determinate what value it finds, so you get eccentric behaviour from the code. You need something more like this, where the newline helps ensure that the output is actually seen.

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

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

回答by NoobBG

I used this very often in my university classes so this should work fine in Visual Studio (tested in VS2013):

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

int main()
{
  char name[64];
  printf("Enter your name: ");
  gets(name);
  printf("Your name is %s\n", name);
 return 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] = '
#include <stdio.h> 
int main(void) { 
    char name[64]; 
    scanf ("%63s", name); 
    printf("Your name is %s", name); 
    return 0; 
}
' // if the number of the actually read symbols is < 63 then '
    #include<stdio.h>

    int main()
    {
      char name[64];

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

     return 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()

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

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

回答by Sai Teja

##代码##

回答by M-N

you should do this : scanf ("%63s", name);

你应该做这个 : scanf ("%63s", name);

Update:

更新

The below code worked for me:

以下代码对我有用:

##代码##

if you are using visual studio, go to Project properties -> Configuration Properties -> C/C++-> Preprocessor -> Preprocessor Definitionsclick on editand add _CRT_SECURE_NO_WARNINGSclick ok, apply the settings and run again.

如果您使用的是visual studio,请Project properties -> Configuration Properties -> C/C++-> Preprocessor -> Preprocessor Definitions单击edit并添加_CRT_SECURE_NO_WARNINGS单击确定,应用设置并再次运行。

Note: this is only good if you are doing your homework or something like that and it's not recommended for production.

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

回答by wilson

##代码##