Thread: Difference between scanf and fgets

  1. #16
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by jimblumberg View Post
    What happens when you have different length strings?

    A different macro for each of these different sizes?
    Blecccch!

    Anduril's previous post noted in passing an approach to that (which I extract here, with lots of snipping before and after the relevant bit).

    Quote Originally Posted by anduril462 View Post
    You can get around that with some macro string quoting tricks or by sprintf'ing your format string into another string first.
    Not the bit after the words "or by".

    I'd still prefer a fgets() and scan the string approach.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  2. #17
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    It would usually be done something like this.
    Code:
    #include <stdio.h>
    
    #define NAME_LEN 10        /* small value for testing */
    
    #define STR2(n) #n         /* stringize */
    #define STR(n) STR2(n)     /* expand n  */
    
    int main(void) {
        char name[NAME_LEN];
    
        scanf("%" STR(NAME_LEN-1) "[^\n]", name);
        printf("<%s>\n", name);
    
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #18
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    What it really boils down to is: how can you reduce the work required to make a simple change like changing the size of a string. Think not just of changing the value of the macro, but if you change the macro used in the definition:
    Code:
    #define FOO_LEN  5
    #define BAR_LEN  10
    char buf[FOO_LEN];  // changing this to BAR_LEN should not require any other changes
    Using the sizeof operator is the only easy way to do this. If you can't use the sizeof operator here, you are at a disadvantage when it comes to maintaining your code, you will have to make more than one change if you change the size of buf.

    The macro expansion to stringify the size of the buffer and concatenate it into the format string fails in this regard. You must change the definition and the scanf call.

    If you sprintf a format string, you can use the sizeof operator. But, since (as Jim pointed out) scanf does not allow for the null character, you either have to remember to do sizeof - 1. It's asking for an off-by-one error somewhere.
    Last edited by anduril462; 09-11-2013 at 12:04 PM. Reason: wording fixes

  4. #19
    Registered User blob84's Avatar
    Join Date
    Jun 2010
    Posts
    46
    This is fgets:
    Code:
    /* fgets:  get at most n chars from iop */
       char *fgets(char *s, int n, FILE *iop)
       {
           register int c;
           register char *cs;
           cs = s;
           while (--n > 0 && (c = getc(iop)) != EOF)
               if ((*cs++ = c) == '\n')
                   break;
           *cs = '\0';
           return (c == EOF && cs == s) ? NULL : s;
       }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets and scanf
    By kkk in forum C Programming
    Replies: 10
    Last Post: 07-29-2011, 10:11 AM
  2. difference between gets and fgets here???
    By n3cr0_l0rd in forum C Programming
    Replies: 4
    Last Post: 03-18-2009, 02:37 PM
  3. scanf vs fgets?
    By Matus in forum C Programming
    Replies: 65
    Last Post: 11-17-2008, 04:02 PM
  4. what happens after 'fgets' and 'scanf'
    By the bassinvader in forum C Programming
    Replies: 4
    Last Post: 07-30-2006, 03:04 PM
  5. Scanf->Fgets
    By 00Sven in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 02:39 PM

Tags for this Thread