fgets() and scanf() peculiarity

Discussion in 'C Programming' started by Eirik, Dec 10, 2003.

  1. Eirik

    Eirik Guest

    Hey, all groovy C programmers,

    I've read in the FAQ(question 12.18) about how applications skip calls to
    (f)gets() after scanf() has been used. How can I avoid this? I know that I
    can by putting the fgets() before the scanf(). However, this one is not
    always suitable. Do you know of any other ways of avoiding this problem?

    --
    - Fordi det rotar til måten folk
    vanlegvis les tekst på.
    - Kvifor?
    - Topp-posting.
    - Kva er det verste du veit om
    i elektronisk post og njus?
     
    Eirik, Dec 10, 2003
    #1
    1. Advertising

  2. Eirik

    Eric Sosman Guest

    Eirik wrote:
    >
    > Hey, all groovy C programmers,
    >
    > I've read in the FAQ(question 12.18) about how applications skip calls to
    > (f)gets() after scanf() has been used. How can I avoid this? I know that I
    > can by putting the fgets() before the scanf(). However, this one is not
    > always suitable. Do you know of any other ways of avoiding this problem?


    One way is to use fgets() -- not gets()! -- to read
    a whole line into a character array, then use sscanf()
    to pick it apart.

    --
     
    Eric Sosman, Dec 10, 2003
    #2
    1. Advertising

  3. Eirik

    Alan Balmer Guest

    On Wed, 10 Dec 2003 21:02:12 +0100, "Eirik"
    <> wrote:

    >Hey, all groovy C programmers,
    >
    >I've read in the FAQ(question 12.18) about how applications skip calls to
    >(f)gets() after scanf() has been used. How can I avoid this? I know that I
    >can by putting the fgets() before the scanf(). However, this one is not
    >always suitable. Do you know of any other ways of avoiding this problem?


    Read it again. Notice that it doesn't say that the call to gets() is
    actually skipped, only that it may appear so. Understand why this
    happens, and you'll have your answer.

    --
    Al Balmer
    Balmer Consulting
     
    Alan Balmer, Dec 10, 2003
    #3
  4. Eirik

    Eirik Guest

    > > Hey, all groovy C programmers,
    > >
    > > I've read in the FAQ(question 12.18) about how applications skip calls

    to
    > > (f)gets() after scanf() has been used. How can I avoid this? I know that

    I
    > > can by putting the fgets() before the scanf(). However, this one is not
    > > always suitable. Do you know of any other ways of avoiding this problem?

    >
    > One way is to use fgets() -- not gets()! -- to read
    > a whole line into a character array, then use sscanf()
    > to pick it apart.


    Can you explain this with a piece of code?
    >
    > --
    >
     
    Eirik, Dec 10, 2003
    #4
  5. "Eirik" <> wrote:
    > Hey, all groovy C programmers,
    >
    > I've read in the FAQ(question 12.18) about how applications skip calls to
    > (f)gets() after scanf() has been used. How can I avoid this? I know that I
    > can by putting the fgets() before the scanf(). However, this one is not
    > always suitable. Do you know of any other ways of avoiding this problem?


    The call to fgets isn't skipped, but returns immediately when it hits
    a newline character that was left in the input stream by a previous
    invokation of scanf. One way to avoid this is to 'drain' the input
    buffer before fgets is called:

    int drain_stdin( void )
    {
    int c;
    while ( ( c = getchar() ) != '\n' && c != EOF )
    ;
    return c;
    }

    .....
    .....
    scanf( .... );
    if ( drain_stdin() != EOF )
    fgets( .... );
    .....
    .....

    Or, you can replace the call to scanf with fgets to read an entire
    line of input and then parse it. (Look up the sscanf, strto*, strchr,
    strcspn, strpbrk, strrchr, strspn, strstr, strtok, is*, strncpy, ...
    functions in your library reference manual.)

    Regards
    --
    Irrwahn Grausewitz ()
    welcome to clc : http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html
    clc faq-list : http://www.eskimo.com/~scs/C-faq/top.html
    acllc-c++ faq : http://www.contrib.andrew.cmu.edu/~ajo/faqs/acllc-c .html
     
    Irrwahn Grausewitz, Dec 11, 2003
    #5
  6. Eirik

    Eric Sosman Guest

    Eirik wrote:
    >
    > > > Hey, all groovy C programmers,
    > > >
    > > > I've read in the FAQ(question 12.18) about how applications skip calls

    > to
    > > > (f)gets() after scanf() has been used. How can I avoid this? I know that

    > I
    > > > can by putting the fgets() before the scanf(). However, this one is not
    > > > always suitable. Do you know of any other ways of avoiding this problem?

    > >
    > > One way is to use fgets() -- not gets()! -- to read
    > > a whole line into a character array, then use sscanf()
    > > to pick it apart.

    >
    > Can you explain this with a piece of code?


    char buffer[BIG_ENOUGH];
    int hh, mm, dd;

    if (fgets(buffer, sizeof buffer, stdin) == NULL) {
    /* end-of-file or error: do something */
    }
    if (sscanf(buffer, "%d:%d:%d", &hh, &mm, &ss) != 3) {
    /* something strange in the input: do something */
    }
    /* Three numbers have been retrieved from one input
    * line, and the next fgets() will get a fresh line
    */

    Fancier code would check whether fgets() actually read a
    complete line; if `BIG_ENOUGH' is too small, fgets() will
    stop prematurely. Corrective action might be to ignore the
    rest of the line, or to expand a dynamically-allocated
    `buffer' and keep on reading.

    --
     
    Eric Sosman, Dec 11, 2003
    #6
  7. Eric Sosman wrote:

    > Fancier code would check whether fgets() actually read a
    > complete line; if 'BIG_ENOUGH' is too small, fgets() will
    > stop prematurely.


    BIG_ENOUGH is always big enough. Just avoid TOO_MUCH, because TOO_MUCH is
    never enough (cf Steinman, 1981).

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
     
    Richard Heathfield, Dec 12, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roedy Green

    ZipEntry peculiarity

    Roedy Green, Sep 14, 2003, in forum: Java
    Replies:
    2
    Views:
    834
    Roedy Green
    Sep 15, 2003
  2. Diez B. Roggisch

    decorator peculiarity

    Diez B. Roggisch, Dec 17, 2004, in forum: Python
    Replies:
    3
    Views:
    357
    Fredrik Lundh
    Dec 19, 2004
  3. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005?

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Apr 26, 2006, in forum: C Programming
    Replies:
    18
    Views:
    703
    Richard Bos
    May 2, 2006
  4. Xavoux

    gets, fgets, scanf none is safe...

    Xavoux, Dec 2, 2006, in forum: C Programming
    Replies:
    20
    Views:
    1,626
    Keith Thompson
    Dec 2, 2006
  5. Charlie Martin

    Peculiarity of '@' in logging.Formatter

    Charlie Martin, Nov 21, 2011, in forum: Python
    Replies:
    3
    Views:
    180
    Vinay Sajip
    Nov 23, 2011
Loading...

Share This Page