Difference between fgets and fscanf?
Asked Answered
N

4

19

I have a question concerning fgets and fscanf in C. What exactly is the difference between these two? 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. In the first example the "counter" in the while loop will not give the same output as in the second example. When changing the "10" in the fgets function the counter will always give different results. 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?

Nadianadine answered 18/1, 2012 at 21:17 Comment(0)
F
18

The function fgets read until a newline (and also stores it). fscanf with the %s specifier reads until any blank space and doesn't store it...

As a side note, you're not specifying the size of the buffer in scanf and it's unsafe. Try:

fscanf(ptr, "%9s", str)
Fit answered 18/1, 2012 at 21:20 Comment(3)
Thanks for your answer, is this also similar for fputs and fprintf? Or what is the difference between them?Nadianadine
No, those are different. For instance %s doesn't stop at blanks in printf. Read the manual.Fit
fgets reads a general string, and fscanf reads a formatted string. That is the difference!Deathtrap
R
7

fgets reads to a newline. fscanf only reads up to whitespace.

Ruder answered 18/1, 2012 at 21:20 Comment(1)
But their primary function is different.Deathtrap
I
7

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. 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.

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. It will stop reading at the next whitespace character or 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>". If you used fgets to read it, str would contain "\t abcdef\n\0". If you usedfscanf, str could contain "abcdef\0" (where \0 indicates the 0 terminator).

Imparipinnate answered 18/1, 2012 at 23:23 Comment(0)
U
2

fgets read the whole line. fscanf with %s read a string, separate by space (or \n,\t,etc...). 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. Note that fgets and scanf don't know how much bytes to read. you should tell them. changing the "10" just enlarge the buffer these functions write to.

Uninstructed answered 18/1, 2012 at 21:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.