Thread: fgets problem..

  1. #1
    5|-|1+|-|34|) ober's Avatar
    Join Date
    Aug 2001
    Posts
    4,429

    fgets problem..

    hey all,

    I'm writing an embedded SQL C program and I have a really stupid problem.

    I'm using the following to get some input from the user:

    Code:
    fgets(artist_name_c, sizeof(artist_name_c), stdin)
    it then is supposed to do some SQL work and come back. However, when it gets to that line, it just goes right through it as if the user has simply pressed enter and entered nothing. I know the SQL code is done correctly and the "artist_name_c" has been declared correctly. I've also tried using "gets" but I get the same result.

    Does anyone know why? If I use scanf it works, but if you put more than one word in, it treats it like 2 seperate words and puts two entries in my DB. help?

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Have you tried: fflush(stdin) before using the fgets function?

  3. #3
    5|-|1+|-|34|) ober's Avatar
    Join Date
    Aug 2001
    Posts
    4,429
    no, i haven't.... i will try that.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Have you tried: fflush(stdin) before using the fgets function?
    Have you ever experienced undefined behavior before? You will if you try this. Use a loop to clear the input buffer instead:

    while ( getchar() != '\n' );

    -Prelude
    My best code is written with the delete key.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Have you tried: fflush(stdin) before using the fgets function?
    Right answer - wrong solution

    This is a simple solution
    Code:
    /* throw away chars up to the next newline */
    while ( getchar() != '\n' );
    > Does anyone know why?
    Because you're mixing input methods - namely using scanf as well as fgets

    scanf just loves to leave lots of newlines lying around on the input stream. This isn't a problem to scanf (it knows to skip them), but to everyone else (fgets), they're a PITA

    Typically, I always use fgets for all input, and if appropriate, sscanf for working out the contents of the resultant buffer.

  6. #6
    5|-|1+|-|34|) ober's Avatar
    Join Date
    Aug 2001
    Posts
    4,429
    thanks Salem and Prelude. I will try that. You guys rock.

  7. #7
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by Prelude
    >Have you tried: fflush(stdin) before using the fgets function?
    Have you ever experienced undefined behavior before? You will if you try this.
    -Prelude
    Can you (or Salem) tell me what's wrong using the fflush function because I'd really like to know.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Monster

    Can you (or Salem) tell me what's wrong using the fflush function because I'd really like to know.
    Read it here
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    That's strange, most of the help files I use say you can flush the stdin buffer.
    But then again... You're the experts.

    Thanks Hammer.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > most of the help files I use say you can flush the stdin buffer
    A common problem for people learning using PC based C compilers - especially those provided by MS/Borland

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Can you (or Salem) tell me what's wrong using the fflush function because I'd really like to know.
    I don't suppose saying that it's bad is good enough? Very well, the fflush function is defined by the ISO C standard as working on output streams. Input streams remain undefined:
    Code:
    7.9.5.2 The fflush function
    
    Synopsis
             #include <stdio.h>
             int fflush(FILE *stream);
    
    Description
    
      If stream points to an output stream or an update stream in which the most recent operation
    was not input, the fflush function causes any unwritten data for that stream to be delivered to
    the host environment to be written to the file; otherwise, the behavior is undefined.
    
      If stream is a null pointer, the fflush function performs this flushing action on all streams
    for which the behavior is defined above.
    
    Returns
    
      The fflush function returns EOF if a write error occurs, otherwise zero.
    >That's strange, most of the help files I use say you can flush the stdin buffer.
    The people who wrote them aren't aware that there's a standard for C. This seems to be a common illness, usually found in the field of teaching and book writing, but certainly not exclusive to those areas.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with fgets....
    By Huskar in forum C Programming
    Replies: 5
    Last Post: 03-29-2009, 10:13 AM
  2. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  3. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 08:10 AM
  4. print problem while using fgets()
    By learninC in forum C Programming
    Replies: 12
    Last Post: 05-15-2005, 09:29 PM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM