Difference between scanf() and fgets()
Asked Answered
C

6

43

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

Chlorate answered 9/8, 2009 at 20:6 Comment(0)
S
30

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.

Samella answered 9/8, 2009 at 21:43 Comment(0)
J
8
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.

Jaconet answered 9/8, 2009 at 21:52 Comment(0)
S
7

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)

Superorder answered 9/8, 2009 at 20:12 Comment(1)
For numeric types, scanf() does not need to do bounds checking. For string types, you can tell scanf() to do boundary checking.Samella
G
2

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.

Gomes answered 21/11, 2013 at 2:57 Comment(0)
M
1

scanf parses a string you read in (or created), and fgets reads a line from an open FILE*. Or do you mean fscanf?

Moro answered 9/8, 2009 at 20:23 Comment(0)
E
1

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.

Engler answered 4/1, 2012 at 15:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.