获取(变量)
谁能告诉我为什么 gets(abc) 适用于 char[] 而不适用于 int?
int abc;
char name[] = "lolrofl";
printf("Hello %s.\n",name);
printf("\n >> ");
fflush(stdin);
gets (abc);
printf("\n die zahl ist %i.\n",abc);
system("Pause");
return(0);
can anyone tell me why gets(abc) works with char[] but not with int?
int abc;
char name[] = "lolrofl";
printf("Hello %s.\n",name);
printf("\n >> ");
fflush(stdin);
gets (abc);
printf("\n die zahl ist %i.\n",abc);
system("Pause");
return(0);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
gets() 的原型是:
请注意,该函数不只读取一个字符并将其放入 s 中;它实际上将整个字符串读入 s 中。然而,由于 gets() 没有提供指定要读取的最大字符数的方法,因此实际上可以将比为 s 分配的字节数更多的字符读入 s 中。因此,这个函数是一个严重的缓冲区溢出漏洞,你永远不应该使用这个函数。还有一些更安全的替代函数可以让您读取用户的输入,例如 fgets () 和 getc()。
如果您使用 C++,则使用 C++ I/O 流库 (std::cin 、 std::cout、std::ostream、std::istream、std::fstream 等)是比使用这些其他函数更好的执行输入/输出的方法。
事实上,函数 gets() 非常危险,以至于在我的 开发和编码自定义搜索引擎,我在 gets 和其他几个此类函数警告不要使用它!
The prototype for gets() is:
Note that the function DOES NOT read just a single character and place it in s; it actually reads an entire string into s. However, since gets() does not provide a way of specifying the maximum number of characters to read, this can actually read more characters into s than there are bytes allocated for s. Thus, this function is a serious buffer overflow vulnerability, and you should not use this function, ever. There are alternative, safer functions which allow you to read input from the user such as fgets() and getc().
If you are using C++, then using the C++ I/O Stream Library (std::cin, std::cout, std::ostream, std::istream, std::fstream, etc.) is a far better way to perform input/output than using these other functions.
The function gets() is so dangerous, in fact, that in my development and coding custom search engine, I have taken out a promotion on gets and several other such functions warning not to use it!
因为它只读取字符。使用 scanf() 进行格式化读取。
顺便说一句,由于您似乎正在使用 C++(或者至少您选择的标签是这样说的),也许您应该尝试 std::cin/std::cout。
Because it only reads characters. Use scanf() for formatted reading.
By the way, since you appear to be using C++ (or at least your choice of tags says so), perhaps you should try std::cin/std::cout.
如果您看一下 C Reference,您的问题将会得到解答。我给你贴一下:
因此,您将无法将整个字符串转换为整数。
If you take a look at the C Reference your question will be answered. I'll paste it for you:
So you won't be able to cast a whole string to an integer.
首先,
gets
函数用于读取字符串 或文本,而不是数字。其次,不要使用
gets
,因为它会出现缓冲区溢出错误。有关详细信息,请参阅C 语言常见问题解答。函数fgets
是一个更安全的替代方案。第三,您可能想要切换到 C++ 流和
std::string
。 C++ 流比 C 流更加类型友好。第四,fflush 不适用于输入流。
fflush
函数用于将流缓冲区中的剩余数据写入输出流。在 C++ 中,有一个方法ignore
,它将忽略传入的字符,直到读取换行符(默认)或指定的字符(或达到限制)。希望有帮助。
First, the
gets
function is for reading strings or text, not numbers.Second, don't use
gets
as it has buffer overrun errors. See C Language FAQ for more information. The functionfgets
is a safer alternative.Third, you may want to switch to C++ streams and
std::string
. The C++ streams are more type friendly than C streams.Fourth,
fflush
does not function on input streams. Thefflush
function is for writing the remaining data in stream buffers to the output stream. In C++, there is a method,ignore
, which will ignore incoming characters until a newline (default) or a specified character is read (or a limit is reached).Hope that helps.