Thread: scanf vs fgets?

  1. #1
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235

    Talking scanf vs fgets?

    What would you guys recommend best for adaptation for future stuff. Iv read on various resources that scanf has its probs and such, and that its wiser to use fgets along with the string scanf for better input of users. Do u guys agree? Recommend it??

    O btw Practical C Programming uses the fgets approach, which is quite ok the way i see it. Just wanna know ur responses. And why it would be or not be ok.
    Last edited by Matus; 11-14-2008 at 10:52 AM.
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    There is no bounds checking in scanf() so it doesn't know where to stop and the program can abend with sigsegv so better to use fgets() for reading the input stream followed by using sscanf() for parsing it.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    There once was a guy named Matus,
    Who pondered, "Which better, scanf or fgets?"
    The folks on the forum knew best
    and without too much foray or jest
    They recommended the use of fgets.
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by Dino View Post
    There once was a guy named Matus,
    Who pondered, "Which better, scanf or fgets?"
    The folks on the forum knew best
    and without too much foray or jest
    They recommended the use of fgets.
    Lol, cool ryhmes man, i need a partner for my upcoming book with poems, lol, care to send one if u write , good to know, im switching to fgets now though
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  5. #5
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by itCbitC View Post
    There is no bounds checking in scanf() so it doesn't know where to stop and the program can abend with sigsegv so better to use fgets() for reading the input stream followed by using sscanf() for parsing it.
    Yep yep got it! Let see more replies,
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    scanf is much more sophisticated than fgets. You cannot really compare them. I mean you can combine sscanf and fgets to do what you want with safety. I don't think there is a better solution than that...

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by Dino View Post
    There once was a guy named Matus,
    Who pondered, "Which better, scanf or fgets?"
    The folks on the forum knew best
    and without too much foray or jest
    They recommended the use of fgets.
    Oh come on now, that should for sure go on your top quotes.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >What would you guys recommend best for adaptation for future stuff.
    It depends on what you're doing and how experienced you are with scanf. I usually recommend fgets and sscanf because too many people have no clue how to properly use scanf, but those same people probably wouldn't understand the better recommendation of fgets and a tailored parsing scheme. However, fgets+sscanf doesn't solve the problem of not fully understanding scanf (because sscanf has basically the same behavior), it just makes it easier to use by decoupling what are really two separate tasks: reading input and parsing input.

    I'm not sure what you've read, but the reality is that scanf is too complicated and quirky to be a good choice in bulletproof code. Ideally you would use a rough equivalent to fgets followed by a parsing algorithm using something solid, strtol or strtod for example.

    So I don't agree with your book, because most of scanf's problems still exist with the fgets+sscanf pairing. But I also don't agree that scanf itself is the problem. It's the programmer using scanf who doesn't know how to use it correctly.

    Where I work there's a joke rule: If you haven't implemented scanf, you're not allowed to use it.

    >There is no bounds checking in scanf()
    Incorrect. There is bounds checking in scanf, but it's not automagic as many seem to expect. The following will never overflow the array:
    Code:
    char buf[256];
    
    scanf ( "%255s", buf );
    My best code is written with the delete key.

  9. #9
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by Prelude View Post
    >What would you guys recommend best for adaptation for future stuff.
    Where I work there's a joke rule: If you haven't implemented scanf, you're not allowed to use it.

    >There is no bounds checking in scanf()
    Incorrect. There is bounds checking in scanf, but it's not automagic as many seem to expect. The following will never overflow the array:
    Code:
    char buf[256];
    
    scanf ( "%255s", buf );
    Hmm ya i guess i have to agree with you Prelude, i guess as time goes by, ill be able to learn stuff. But thanks for the comments.
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  10. #10
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by master5001 View Post
    Oh come on now, that should for sure go on your top quotes.
    Yup sure does, i laughed when i read it!
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You should hear what he wrote about the girl from Regina.

  12. #12
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    The girl from Regina knew action
    She had a huge and hairy contraption
    All the boys knew
    What she carried wasn't the flu
    But that didn't stop the attraction
    Mainframe assembler programmer by trade. C coder when I can.

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Lol! By the way that is way worse than a reply I posted a couple hours ago and Salem deleted mine.

  14. #14
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by master5001 View Post
    You should hear what he wrote about the girl from Regina.
    haha this dude should go for rapping. Umm hey so take this for instance:


    Code:
    char temp[50];
    int i;
    int j;
    int m;
    
    
    fgets(temp,sizeof(temp),stdin);
    sscanf(temp,"%d",&i);
    
    and then i could use that same temp for other vairable right, cuz its just a temp. For instance if later i wanted to read info for int j, int m etc.
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  15. #15
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Yep. though if you wanted to do that I would recommend just saying j = m = i; after reading it in. Did you get my email?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what happens after 'fgets' and 'scanf'
    By the bassinvader in forum C Programming
    Replies: 4
    Last Post: 07-30-2006, 03:04 PM
  2. is scanf as safe as fgets when used to receive string?
    By Antigloss in forum C Programming
    Replies: 4
    Last Post: 08-31-2005, 05:18 PM
  3. replacing scanf with fgets
    By subflood in forum C Programming
    Replies: 6
    Last Post: 08-19-2004, 01:59 PM
  4. replacing scanf with fgets
    By guest73 in forum C Programming
    Replies: 8
    Last Post: 09-16-2002, 04:52 AM
  5. String manipulation and scanf vs fgets
    By Nit in forum C Programming
    Replies: 9
    Last Post: 03-20-2002, 12:44 PM