fgets not working after fgetc

This is a discussion on fgets not working after fgetc within the C Programming forums, part of the General Programming Boards category; I have code and I first retrieve a character using fgetc (using the file pointer of "stdin"). Then after a ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    15

    fgets not working after fgetc

    I have code and I first retrieve a character using fgetc (using the file pointer of "stdin"). Then after a couple of printf functions, I try to do fgets from "stdin" but the prompt doesn't come up.

    I took out the fgetc to make sure my syntax and use of fgets was not wrong, and fgets worked when I editted out fgetc. When I put fgetc back in before the fgets, the fgets function didn't work again.

    What could be causing this?? Any fix for this?

    Thanks.

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    "The prompt doesn't come back up again" If I'm interpreting you correctly, your program isn't prompting the user for input. This is probably because there already is input, from when it prompted the user during the first fgetc(). Something like this is probably occuring:
    fgetc() called: user inputs text...
    fgets() called, data still to be read from stdin, that data is read, user not prompted.
    Remember: You might even be typing just "a<enter>" but that <enter> does come through.
    Ask youself: What does fgets() read in from stdin? (And check? Is it really nothing, or whitespace, such as a newline?)

    Edit: Posting code helps get a more exact answer. Otherwise, we have to base our help on assumptions.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    >What could be causing this??
    It sounds like the Enter key got stuck in your stream. When you use fgetc or any of its buddies to read a single character, the stream actually gets two characters because you have to hit Enter:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      int c;
    
      printf ( "Type one character: " );
      fflush ( stdout );
    
      if ( ( c = fgetc ( stdin ) ) != EOF ) {
        printf ( "You typed %d\n", c );
        printf ( "I also got %d\n", fgetc ( stdin ) );
      }
    
      return 0;
    }
    That's one of the stumbling blocks of C's line oriented streams, but it's easy to fix with a fun little function like this that you can call just before you call fgets:
    Code:
    void flush_input ( FILE *in )
    {
      int c;
    
      while ( ( c = fgetc ( in ) ) != EOF && c != '\n' )
        ;
    
      clearerr ( in );
    }

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    15
    Perfect! Thanks for the help, it's all up and working now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-07-2009, 12:31 PM
  2. fgets returns null
    By strider1974 in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 06:08 AM
  3. Using fgets?
    By cogeek in forum C Programming
    Replies: 3
    Last Post: 12-08-2004, 12:08 PM
  4. help with fgets
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-17-2001, 09:18 PM
  5. fgets & storing
    By hyaline in forum C Programming
    Replies: 1
    Last Post: 09-18-2001, 12:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21