简体   繁体   English

fgets和fscanf之间的区别?

[英]Difference between fgets and fscanf?

I have a question concerning fgets and fscanf in C. What exactly is the difference between these two? 我对C中的fgets和fscanf有疑问。这两者之间到底有什么区别? For example: 例如:

char str[10];
while(fgets(str,10,ptr))
{
counter++;
...

and the second example: 和第二个例子:

char str[10];
while(fscanf(ptr,"%s",str))
{
counter++;
...

when having a text file which contains strings which are separated by an empty space, for example: AB1234 AC5423 AS1433. 当有一个包含由空格分隔的字符串的文本文件时,例如:AB1234 AC5423 AS1433。 In the first example the "counter" in the while loop will not give the same output as in the second example. 在第一个示例中,while循环中的“counter”将不会提供与第二个示例中相同的输出。 When changing the "10" in the fgets function the counter will always give different results. 当更改fgets函数中的“10”时,计数器将始终给出不同的结果。 What is the reason for this? 这是什么原因? Can somebody please also explain what the fscanf exactly does, how long is the string in each while loop? 还有人可以解释一下fscanf究竟做了什么,每个while循环中的字符串有多长?

The function fgets read until a newline (and also stores it). 函数fgets读取直到换行符(并且还存储它)。 fscanf with the %s specifier reads until any blank space and doesn't store it... 带有%s说明符的fscanf读取直到任何空格并且不存储它...

As a side note, you're not specifying the size of the buffer in scanf and it's unsafe. 作为旁注,您没有在scanf中指定缓冲区的大小,并且它不安全。 Try: 尝试:

fscanf(ptr, "%9s", str)

fgets reads to a newline. fgets读取到换行符。 fscanf only reads up to whitespace. fscanf只能读取空格。

In your example, fgets will read up to a maximum of 9 characters from the input stream and save them to str , along with a 0 terminator. 在您的示例中, fgets将从输入流中读取最多9个字符,并将它们与0终止符一起保存到str It will not skip leading whitespace. 它不会跳过前导空格。 It will stop if it sees a newline (which will be saved to str ) or EOF before the maximum number of characters. 如果在最大字符数之前看到换行符(将保存为str )或EOF,它将停止。

fscanf with the %s conversion specifier will skip any leading whitespace, then read all non-whitespace characters, saving them to str followed by a 0 terminator. 带有%s转换说明符的fscanf将跳过任何前导空格,然后读取所有非空白字符,将它们保存为str后跟0终止符。 It will stop reading at the next whitespace character or EOF. 它会在下一个空格字符或EOF处停止读取。 Without an explicit field width, it will read as many non-whitespace characters as are in the stream, potentially overruning the target buffer. 如果没有明确的字段宽度,它将读取与流中的非空白字符一样多的非空白字符,可能会超出目标缓冲区。

So, imagine the input stream looks like this: "\\t abcdef\\n<EOF>" . 因此,假设输入流看起来像这样: "\\t abcdef\\n<EOF>" If you used fgets to read it, str would contain "\\t abcdef\\n\\0" . 如果您使用fgets读取它, str将包含"\\t abcdef\\n\\0" If you used fscanf , str could contain "abcdef\\0" (where \\0 indicates the 0 terminator). 如果使用fscanfstr可能包含"abcdef\\0" (其中\\0表示0终结符)。

fgets read the whole line. fgets读了整行。 fscanf with %s read a string, separate by space (or \\n,\\t,etc...). 带有%s fscanf读取一个字符串,按空格分隔(或\\ n,\\ t等等)。 Anyway, you should not use them unless you sure that the array you read to is big enough to contain the input. 无论如何,你不应该使用它们,除非你确定你读到的数组大到足以包含输入。 You wrote When changing the "10" in the fgets function the counter will always give different results. 您写了When changing the "10" in the fgets function the counter will always give different results. Note that fgets and scanf don't know how much bytes to read. 请注意,fgets和scanf不知道要读取多少字节。 you should tell them. 你应该告诉他们。 changing the "10" just enlarge the buffer these functions write to. 更改“10”只是放大这些函数写入的缓冲区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM