Thread: is scanf as safe as fgets when used to receive string?

  1. #1
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216

    is scanf as safe as fgets when used to receive string?

    as is shown at the following code. are those two statement as safe as each other?
    Code:
    char c[20];
    scanf( "%19s", c );
    fgets( c, sizeof(c), stdin );

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    As the are in your code you can consider them the same.
    But if you for some reason make c smaller some time you would also have to change the formatstring. If you forget about that => bang.
    So I think fgets is a little safer.
    Kurt

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    10

    Thumbs up

    Both these functions are not totally safe. The return key causes the problem. Scanf() leaves a newline hanging in your input buffer. The hanging newline character is then possibly picked up by the next scanf().

    Now fgets() simply attaches it to your input string. When you do some processing later, and for instance compare the string with the trailing newline to a given string in your code, you won't get a match!

    I would say fgets() is safer, if you remove the trailing newline! Again here is a sample code that shows you how to do this:
    Code:
    // secure string input, fgets() replaces gets() and scanf()
    
    #include <stdio.h>   // BUFSIZ usually 512
    #include <string.h> 
    
    int main()
    {
      char buf[BUFSIZ] = "";
      char *p;
      
      printf("Please enter a line of text (max %d characters)\n", sizeof(buf));
      
      if (fgets(buf, sizeof(buf), stdin) != NULL)
      {
        // remove trailing \n 
        if ((p = strchr(buf, '\n')) != NULL)
          *p = '\0';
        printf("you entered: %s\n", buf);      
      }
    
      getchar();  // wait  
      return 0;
    }
    Ask smart questions - if I would be that smart, I wouldn't need to ask questions.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Both these functions are not totally safe.
    Yes, fgets() is safe when used correctly.

    The problem you describe (newline in the buffer) isn't a safety issue, it's a programming issue. If you don't want a newline, then remove it (as you describe). If you simply don't care about the newline then there's nothing else to do.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    Both these functions are not totally safe.
    You're confusing 'unsafe' with 'potentially confusing semantics'. An unsafe function can blow up in your face, but a function with potentially confusing semantics is perfectly safe when you use it right.
    Scanf() leaves a newline hanging in your input buffer.
    scanf() leaves any whitespace hanging in the input buffer. That's only a problem if you mix scanf() with functions or format specifiers that don't strip leading whitespace, and the code isn't written to handle it. Code that isn't written to handle unexpected whitespace gracefully is usually sloppier than my 2 year old niece eating Spaghetti-Os.
    Just because I don't care doesn't mean I don't understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. replacing scanf with fgets
    By guest73 in forum C Programming
    Replies: 8
    Last Post: 09-16-2002, 04:52 AM
  5. Problem with fgets and getting the string
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 10-03-2001, 02:22 PM