scanf() 和 fgets() 之间的区别

发布于 2024-07-30 02:59:35 字数 79 浏览 6 评论 0原文

我想知道 fgets()scanf() 之间有什么区别。 我使用 C 作为我的平台。

I want to know what is the difference between fgets() and scanf(). I am using C as my platform.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

阳光①夏 2024-08-06 02:59:35

有多个差异。 两个关键点是:

  • fgets() 可以从任何打开的文件中读取,但 scanf() 只能读取标准输入。
  • fgets() 从文件中读取“一行文本”; scanf() 可以用于此目的,但也可以处理从字符串到内置数字类型的转换。

许多人会使用fgets()读取一行数据,然后使用sscanf()对其进行剖析。

There are multiple differences. Two crucial ones are:

  • fgets() can read from any open file, but scanf() 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 use sscanf() to dissect it.

欢烬 2024-08-06 02:59:35
int scanf(const char * restrict format, ...);

scanf(3) 搜索由给定输入(称为 stdin)上的 格式参数 定义的特定模式,其中模式由您定义。 scanf(3) 的给定输入可以是字符串或文件,具体取决于其变体(scanf、fscanf、sscanf、vscanf、vsscanf、vfscanf)。

char *fgets(char * restrict str, int size, FILE * restrict stream);

fgets(3) 只是从输入文件中读取,并将字节作为空终止字符串复制到缓冲区str< /em> 并将缓冲区的输出限制为大小中的给定字节。

int scanf(const char * restrict format, ...);

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.

char *fgets(char * restrict str, int size, FILE * restrict stream);

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.

不知在何时 2024-08-06 02:59:35

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)

仅一夜美梦 2024-08-06 02:59:35

应该注意的是,scanf 模式规范确实允许字段宽度限制:

scanf( " %80s", mybuffer );

但是,printf() 允许将宽度作为变量传递(带有“*”):

printf( "My name is %*s.\n", 20, name );

scanf() 没有。 (它将“*”解释为完全抑制/忽略该字段的标志。)这意味着您最终会做这样的事情:

#define NAMEWIDTH 40
char buffer[ NAMEWIDTH + 4 ];
...
scanf( " %40x", buffer );

并且无法在 scanf() 中连接字段宽度 40缓冲区声明中的缓冲区宽度为 40。

It should be noted that scanf pattern specs do allow field width limits:

scanf( " %80s", mybuffer );

But, where printf() allows the width to be passed as a variable (with '*'):

printf( "My name is %*s.\n", 20, name );

scanf() does not. (It interprets the '*' as a flag to suppress/ignore the field entirely.) Which means you end up doing things like this:

#define NAMEWIDTH 40
char buffer[ NAMEWIDTH + 4 ];
...
scanf( " %40x", buffer );

and no way to connect the field width 40 in the scanf() with the buffer width 40 in the buffer declaration.

江南月 2024-08-06 02:59:35

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?

瘫痪情歌 2024-08-06 02:59:35

主要区别在于 scanf 对可读取的字符数没有限制(默认使用情况下),而 fgets 具有可读取的最大字符数。

看两个函数的原型:

char * fgets(char * dest, int size, FILE * Stream);

int scanf(const char * format,...);

可以看到第二个参数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), while fgets has a maximum number of char that can be read.

See the prototype of the two functions:

char * fgets (char * dest, int size, FILE * stream);

int scanf (const char * format, ...);

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 to dest 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 reads 12345\n, the output is 12345, while the fgets reads and returns till \n (included), adding a \0 in the end as a string terminator.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文