- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
The most recent revision of the C standard (2011) has definitively removed this function from its specification
The function is deprecated in C++ (as of 2011 standard, which follows C99+TC3).
我只是想知道 C11 标准中 gets()
的替代方法是什么?
最佳答案
在 C11 中,gets
已替换为具有以下声明的 gets_s
:
char *gets_s(char *str, rsize_t n);
此函数将从stdin
读取最多n-1
个字符到*str
。这是为了避免 gets
固有的缓冲区溢出漏洞。函数 fgets
也是一个选项。来自 http://en.cppreference.com/w/c/io/gets :
The gets() function does not perform bounds checking, therefore this function is extremely vulnerable to buffer-overflow attacks. It cannot be used safely (unless the program runs in an environment which restricts what can appear on stdin). For this reason, the function has been deprecated in the third corrigendum to the C99 standard and removed altogether in the C11 standard. fgets() and gets_s() are the recommended replacements.
Never use gets().
鉴于 gets_s
是在标准的扩展中定义的,只能选择性地实现,您可能应该改用 fgets
编写程序。如果您在 stdin
上使用 fgets
,您的程序也将在早期版本的 C 中编译。但请记住行为上的差异:当 gets_s
已读取 n-1
个字符它会一直读取直到到达新行或文件末尾,然后丢弃输入。因此,使用 gets_s
,您总是读取整行,即使输入缓冲区中只能返回其中的一部分。
关于c - C11 中的 gets() 等价物是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12893774/
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
用作 mergetool for Git 时,vimdiff 中与 kdiff3 的“从 A/B/C 中选择行”等效的是什么? kdiff3 中是否有类似 Ctrl+1/2/3 的快捷方式? 最佳答案
什么是 Javascript 等同于 imgsrc = $("#content div form img").attr('src'); HTML 是
您好,我有一个数据库可以从中选择 IP 位置> 脚本是在 php 中,我正在将它转换为 java,但我不知道什么是 ip2long('127.0.0.1' )); 在 java 中的等价物 最佳答案
我有一个 C# 应用程序,我正试图将其转换为 Java。 C# 应用程序有几个类型为 ushort 的变量。 Java 中是否有等效项? 谢谢 最佳答案 在大小方面最接近的等价物是 char,因为 J
我正在 iOS 中寻找与 .NET 中的脉冲和等待模式相同的多线程模式。本质上,我希望后台线程处于休眠状态,直到设置标志为止,这实际上是将线程“踢”到行动中。 它是 loop+thread.sle
对于某些并发编程,我可以使用 Java 的 CountDownLatch概念。是否有 C++11 的等效项,或者该概念在 C++ 中称为什么? 我想要的是在计数达到零时调用一个函数。 如果还没有,我会
我正在用 Ruby 开发一个 CLI 应用程序,我想允许通过 /etc/appnamerc 的标准配置文件级联在 Unix 中进行配置。 , ~/.appnamerc .但是,该应用程序也意味着在 W
是否有与 JAXB 等效的 PHP?它被证明对 Java 开发非常有用,作为一个 PHP 新手,我想在 PHP 世界中使用 JAXB 提供的相同概念。 最佳答案 我之前也想找同样的东西,但是找不到。所
Python 有一个 urljoin 函数,它接受两个 URL 并智能地连接它们。有没有在c++中提供类似功能的库? urljoin 文档:http://docs.python.org/library
我有一个从另一种语言移植的功能,你能帮我把它变成“pythonic”吗? 这里的函数以“非pythonic”方式移植(这是一个有点人为的例子 - 每个任务都与一个项目相关联或“无”,我们需要一个不同项
我有 2 个相同类型的对象,我想将一种状态浅复制到另一种状态。在 C++ 中,我有很棒的 memcpy。我怎样才能在 C# 中做到这一点? MemberwiseClone() 不够好,因为它创建并返回
有什么方法可以在 CSS 中使用条件语句吗? 最佳答案 我想说 CSS 中最接近“IF”的是媒体查询,例如可用于响应式设计的媒体查询。对于媒体查询,您是在说“如果屏幕宽度在 440 像素到 660 像
我正在尝试在 Swift 的 iTunesU 中从“为 iphone 和 ipad 开发 ios7 应用程序”中复制 Stanford Matchismo 游戏。 第三讲77页slides ,它显示使
这个问题在这里已经有了答案: Store output of subprocess.Popen call in a string [duplicate] (15 个回答) 关闭4年前。 我想从 pyt
这个问题在这里已经有了答案: Is there a 'foreach' function in Python 3? (14 个回答) 关闭1年前。 我正在深入研究 Python,但我有一个关于 for
我想从 Java 中的这个 Kotlin 类访问信息。它是通过 Gradle 库导入的。 密封类: public sealed class Resource private constructor()
SWT 中的 JPanel 有什么等价物? 最佳答案 原始问题要求 SWT 等同于 JLabel。 还有一个 org.eclipse.swt.custom.CLabel . SWT 等价于 JPane
在诸如 postgres 之类的 SQL 数据库中,我们可以创建 SCHEMA,以便我们可以将我们的表作为 schema_name.table_name 引用。 mongodb 中有模式吗?谢谢 最佳
哪个模型是“GBTRegressor”Pyspark 模型的 Python 等效模型? 简要背景:我正在尝试将 pyspark 模型重新创建为 python 模型。现有管道中使用的模型是 GBTReg
我是一名优秀的程序员,十分优秀!