关于scanf的超级愚蠢问题 [英] Super dumb question about scanf

查看:75
本文介绍了关于scanf的超级愚蠢问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我对这个基本问题感到羞耻,但我必须这样做。



我有一个简单的程序,它只是在控制台上写一行,然后从中读取。我注意到我的scanf没有得到我在控制台中写的字符串,并没有把它放在someStr中。 someStr对我来说总是空的。我正在使用VS 2013.



有人可以帮我理解我做错了什么。我在线搜索&感觉我的代码是正确的:)



谢谢!



# include   stdafx.h 
#include stdio.h
#include string.h

const int MAX_STR = 256 ;

// int _tmain(int argc,_TCHAR * argv [])
int main( void
{
char someStr [MAX_STR],someOtherStr [MAX_STR];
printf( 输入字符串:);
scanf_s( %s,someStr);

return 0 ;
}

解决方案

您需要在 scanf_s 按照 http://msdn.microsoft.com/en-us/library/w40768et中的说明进行呼叫。 aspx [ ^ ]。


您没有使用 scanf ,您使用的是Microsoft风格的安全版本 scanf ,因此你必须仔细阅读他们的文件。



来自 MSDN [ ^ ]:



与scanf和wscanf不同,scanf_s和wscanf_s要求为所有输入指定缓冲区大小c,c,s,S或字符串控制集的参数,包含在[]中。字符的缓冲区大小作为附加参数传递,紧跟在指向缓冲区或变量的指针之后。例如,如果您正在读取字符串,则该字符串的缓冲区大小将按如下方式传递:

 char s [10]; 
scanf_s(%9s,s,_countof(s)); //缓冲区大小为10,宽度规范为9



缓冲区大小包括终止空值。您可以使用宽度指定字段来确保读入的令牌适合缓冲区。如果没有使用宽度指定字段,并且读入的令牌太大而无法放入缓冲区,则不会向该缓冲区写入任何内容。


根据我的记忆,您需要使用& 前缀将变量的地址传递给 scanf

< pre lang =c ++> scanf_s( %s,& someStr);


Hello,

I feel ashamed of asking this basic question, but I have to.

I have a simple program which just writes a line on console and then reads from it. I am noticing that my scanf is not getting the string that I wrote in console and doesn't put it in someStr. someStr is always empty for me. I am using VS 2013.

Can someone help me understand what I m doing wrong. I searched online & feel my code is correct :)

Thanks!

#include "stdafx.h"
#include "stdio.h"
#include "string.h"

const int MAX_STR = 256;

//int _tmain(int argc, _TCHAR* argv[])
int main(void)
{
    char someStr[MAX_STR], someOtherStr[MAX_STR];
    printf("Enter a string: ");
    scanf_s("%s", someStr);

    return 0;
}

解决方案

You need to provide the buffer length in your scanf_s call as described in http://msdn.microsoft.com/en-us/library/w40768et.aspx[^].


You are not using scanf, you are using the Microsoft-flavored 'secure' version of scanf, hence you have to carefully read their documentation.

From MSDN[^]:

Unlike scanf and wscanf, scanf_s and wscanf_s require the buffer size to be specified for all input parameters of type c, C, s, S, or string control sets that are enclosed in []. The buffer size in characters is passed as an additional parameter immediately following the pointer to the buffer or variable. For example, if you are reading a string, the buffer size for that string is passed as follows:

char s[10];
scanf_s("%9s", s, _countof(s)); // buffer size is 10, width specification is 9


The buffer size includes the terminating null. You can use a width specification field to ensure that the token that's read in will fit into the buffer. If no width specification field is used, and the token read in is too big to fit in the buffer, nothing is written to that buffer.


From what I remember you need to pass the address of the variable to scanf with a & prefix:

scanf_s("%s", &someStr);


这篇关于关于scanf的超级愚蠢问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆