scanf() 和 fgets() 之间的区别
我想知道 fgets()
和 scanf()
之间有什么区别。 我使用 C 作为我的平台。
I want to know what is the difference between fgets()
and scanf()
. I am using C as my platform.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
有多个差异。 两个关键点是:
fgets()
可以从任何打开的文件中读取,但scanf()
只能读取标准输入。fgets()
从文件中读取“一行文本”;scanf()
可以用于此目的,但也可以处理从字符串到内置数字类型的转换。许多人会使用
fgets()
读取一行数据,然后使用sscanf()
对其进行剖析。There are multiple differences. Two crucial ones are:
fgets()
can read from any open file, butscanf()
only reads standard input.fgets()
reads 'a line of text' from a file;scanf()
can be used for that but also handles conversions from string to built in numeric types.Many people will use
fgets()
to read a line of data and then usesscanf()
to dissect it.scanf(3) 搜索由给定输入(称为 stdin)上的 格式参数 定义的特定模式,其中模式由您定义。 scanf(3) 的给定输入可以是字符串或文件,具体取决于其变体(scanf、fscanf、sscanf、vscanf、vsscanf、vfscanf)。
fgets(3) 只是从输入文件流中读取行,并将字节作为空终止字符串复制到缓冲区str< /em> 并将缓冲区的输出限制为大小中的给定字节。
scanf(3) searches for certain pattern defined by the format argument on the given input known as stdin, where the pattern is defined by you. The given input to scanf(3), depending on its variant (scanf, fscanf, sscanf, vscanf, vsscanf, vfscanf), could be a string or a file.
fgets(3) just reads a line from the input file stream and copy the bytes as null terminating string to the buffer str and limit the output to the buffer to given bytes in size.
Scanf 不执行边界检查。 fgets 可能是更好的选择。 然后您可以使用 sscanf() 对其进行评估。
这里对这个话题进行了很好的讨论——
http://cboard.cprogramming.com/c-programming/ 109243-scanf-vs-fgets.html
如何允许使用 scanf 输入空格?(那是我邪恶的双胞胎因忘记这一点而受到训斥 - 不是我)
Scanf does not perform bounds checking. fgets is likely going to be the better choice. You can then use sscanf() to evaluate it.
Good discussion of the topic here-
http://cboard.cprogramming.com/c-programming/109243-scanf-vs-fgets.html
How do you allow spaces to be entered using scanf? (That was my evil twin getting lectured for forgetting this- not me)
应该注意的是,
scanf
模式规范确实允许字段宽度限制:但是,
printf()
允许将宽度作为变量传递(带有“*”):scanf()
没有。 (它将“*”解释为完全抑制/忽略该字段的标志。)这意味着您最终会做这样的事情:并且无法在
scanf()
中连接字段宽度 40缓冲区声明中的缓冲区宽度为 40。It should be noted that
scanf
pattern specs do allow field width limits:But, where
printf()
allows the width to be passed as a variable (with '*'):scanf()
does not. (It interprets the '*' as a flag to suppress/ignore the field entirely.) Which means you end up doing things like this:and no way to connect the field width 40 in the
scanf()
with the buffer width 40 in the buffer declaration.scanf 解析您读入(或创建)的字符串,而 fgets 从打开的 FILE* 中读取一行。 或者你的意思是fscanf?
scanf parses a string you read in (or created), and fgets reads a line from an open FILE*. Or do you mean fscanf?
主要区别在于
scanf
对可读取的字符数没有限制(默认使用情况下),而fgets
具有可读取的最大字符数。看两个函数的原型:
可以看到第二个参数
fgets
强制读取最多大小的字符。另一个明显的区别是返回值:
fgets
成功时返回指向dest
的指针;scanf
返回成功匹配和分配的输入项的数量。然后,scanf函数根据格式扫描输入,并从标准输入流stdin读取输入,而
fgets
默认从 FILE * Stream 读取输入。总之,您可以使用
scanf
从 FILE 中读取数据并将它们插入到固定大小的数组中(例如)没有多大意义。scanf
的一个优点是输出数据的格式:如果函数读取12345\n
,则输出为12345
,而 fgets 则读取和返回直到\n
(包含),在末尾添加\0
作为字符串终止符。The main difference lies in the fact that
scanf
has no limits on the number of characters that can be read (in its default use), whilefgets
has a maximum number of char that can be read.See the prototype of the two functions:
You can see that the second parameter of
fgets
imposes to read at most size char.Another clear difference is the return value:
fgets
return a pointer todest
on success;scanf
return the number of input items successefully matched and assigned.Then, the scanf function scans input according to format, and reads input from the standard input stream stdin, while
fgets
reads input from FILE * stream as default.In conclusion, you could use
scanf
to read data from a FILE and insert them into a fixed-size array (for example) does not have much sense.An advantage of
scanf
is the formatting of output data: if the function reads12345\n
, the output is12345
, while the fgets reads and returns till\n
(included), adding a\0
in the end as a string terminator.