944,050 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 35198
  • C RSS
Oct 17th, 2007
0

Scanf vs fgets

Expand Post »
I'm having some trouble understanding the advantages and disadvantages of using scanf over fgets.

When we have allocated memory using an array for eg, it is not wise to use scanf since buffer overflow can occur. But in what way does fgets prevent that from happening?

Also, if you are allocating memory dynamically then is it okay to be using scanf?

I have this code:
  1. typedef struct node
  2. {
  3. int data;
  4. struct node *next;
  5. }NODE;
  6. static NODE *find(NODE *element);
  7.  
  8. void main()
  9. {
  10. NODE *element,*head,*a;
  11. element = (NODE *) malloc (sizeof(NODE));
  12. element->next=head;
  13. printf("Enter the data\n");
  14. scanf("%d",&element->data);
  15. head=element;
  16. a =find(element);
  17. if(a)
  18. {
  19. printf("Element %d was found \n",element->data);
  20. }
  21. else
  22. {
  23. printf("Element %d was not found\n",element->data);
  24.  
  25. }
  26. free(head);
  27. }
  28.  
  29. NODE * find(NODE *element)
  30. {
  31. while(element)
  32. {
  33. if(element->data==50)
  34. {
  35. return element;
  36. }
  37. else
  38. {
  39. return 0;
  40. }
  41. element=element->next;
  42. }
  43.  
  44. }
If I were to use fgets instead of scanf here, how would I incorporate it?

Thanks.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
#include_rose is offline Offline
14 posts
since Jul 2007
Oct 17th, 2007
0

Re: Scanf vs fgets

>I'm having some trouble understanding the advantages
>and disadvantages of using scanf over fgets.
I imagine because the comparison is difficult. scanf and fgets do different things. scanf is designed for formatted input and fgets is designed for unformatted input.

>But in what way does fgets prevent that from happening?
Assuming you use it correctly, the second argument of fgets provides a limit on the number of characters that are read:
  1. char buffer[10];
  2.  
  3. if ( fgets ( buffer, 10, stdin ) != NULL )
  4. fputs ( buffer, stdin );
It doesn't matter how many characters you actually type, only up to 9 will be written to buffer, and the last spot will be '\0'. The same can't be said about a naive use of scanf:
  1. char buffer[10];
  2.  
  3. if ( scanf ( "%s", buffer ) == 1 )
  4. puts ( buffer );
You can type 5000 characters, and as long as there's no whitespace scanf will read 5000 characters. The big question is, where does it write them all if buffer can only hold 10?

That's a naive use of scanf, and if you find yourself doing that, you shouldn't be using scanf at all, because you simply don't understand it well enough to use it safely. You can plug the buffer overflow hole with scanf like this:
  1. char buffer[10];
  2.  
  3. if ( scanf ( "%9s", buffer ) == 1 )
  4. puts ( buffer );
By adding a maximum field width, you're telling scanf to read up to that many characters, and no more. So if you type 5000 character, scanf will only read the first 9, and tack a '\0' onto the last spot.

>Also, if you are allocating memory dynamically then is it okay to be using scanf?
How you get the buffer is irrelevant.

>If I were to use fgets instead of scanf here, how would I incorporate it?
The problem is that scanf reads formatted input and fgets only reads strings. scanf will take "12345\n" and with the %d specifier, convert it into the integer 12345. fgets will just give you "12345\n". To get the integer value with fgets, you need another conversion step. Ironically, sscanf is a good choice for that. So this:
  1. scanf("%d",&element->data);
becomes this:
  1. {
  2. char line[BUFSIZ];
  3.  
  4. if ( fgets ( line, sizeof line, stdin ) == NULL
  5. || sscanf ( line, "%d", &element->data ) != 1 )
  6. {
  7. /* Handle bad input */
  8. }
  9. }
Other methods include the horrible atoi function, the much better strtol, and your own conversion routine. Using scanf for reading anything but string data is actually not that bad. You may have some minor issues handling failure, but most of the conversion specifiers aren't glaringly unsafe like %s.
Last edited by Narue; Oct 17th, 2007 at 5:14 pm.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 17th, 2007
0

Re: Scanf vs fgets

Thanks a lot. That cleared my doubts. :-)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
#include_rose is offline Offline
14 posts
since Jul 2007
Oct 17th, 2007
0

Re: Scanf vs fgets

Quote ...
Ironically, sscanf is a good choice for that. So this:
  1. scanf("%d",&element->data);
becomes this:
  1. {
  2. char line[BUFSIZ];
  3.  
  4. if ( fgets ( line, sizeof line, stdin ) == NULL
  5. || sscanf ( line, "%d", &element->data ) != 1 )
  6. {
  7. /* Handle bad input */
  8. }
  9. }
I didn't understand this though. How would I use fgets when I want to allocate memory dynamically? Here you are allocating static memory using line[BUFSIZ]; Am i right?
Last edited by #include_rose; Oct 17th, 2007 at 10:52 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
#include_rose is offline Offline
14 posts
since Jul 2007
Oct 17th, 2007
0

Re: Scanf vs fgets

I didn't understand this though. How would I use fgets when I want to allocate memory dynamically? Here you are allocating static memory using line[BUFSIZ]; Am i right?
When you used
  1. scanf("%d",&element->data);
element->data is the pointer of what is in dynamic memory.

When you use fgets is the same element->data that is in the heap.
line[BUFSIZ] is a local array to temporarily hold the string needed
by sscanf to convert it into an integer.
That line[BUFSIZ] doesn't need to be in dynamic memory.
Last edited by Aia; Oct 17th, 2007 at 11:27 pm.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Oct 18th, 2007
0

Re: Scanf vs fgets

>How would I use fgets when I want to allocate memory dynamically?
Exactly the same way. Did you miss the part where I said that how you get the buffer is irrelevant? If it really bothers you, is this better? It uses memory dynamically.
  1. {
  2. char *line = malloc ( BUFSIZ );
  3.  
  4. if ( line != NULL ) {
  5. if ( fgets ( line, BUFSIZ, stdin ) == NULL
  6. || sscanf ( line, "%d", &element->data ) != 1 )
  7. {
  8. /* Handle bad input */
  9. }
  10. }
  11.  
  12. free ( line );
  13. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 18th, 2007
0

Re: Scanf vs fgets

For some reason now it feels better
Reputation Points: 10
Solved Threads: 0
Newbie Poster
#include_rose is offline Offline
14 posts
since Jul 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: How to name and create a textfile?
Next Thread in C Forum Timeline: Problem with multiDimensional array?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC