Thread: Fgets not reading stdin. Help please.

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    33

    Fgets not reading stdin. Help please.

    I'm creating a simple function that reads three letters. The problem is, gdb shows me that the printf inquiry occurs after all the fgets and suggested newline elimination loops. Here's what gdb says in a nutshell.

    74 printf("Please enter a subject: ");
    (gdb) s
    75 fgets(input, 5, stdin);
    (gdb) s
    .
    .
    .
    84 printf("%s\n", input);
    (gdb) s
    Please enter a subject:
    .
    .
    .
    So I end up with an infinite number of inquiries. I don't even get the opportunity to enter in any letters. Please Help!

    Code:
    void find_subject( int **crns, char ***subjects, char ***courses,
         int *count )
    {
      char input[20];
      int i;
    
      printf("Please enter a subject: ");
      fgets(input, 5, stdin);
      for ( i = 0 ; i < 20 ; i++ )
      {
        if ( input[i] == '\n' )
        {
            input[i] = '\0';
            break;
        }
      }
      printf("%s\n", input);
    }

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    stdout is usually line-buffered by default, which means you won't see the output until you print a newline character. You can force the buffered output to print by flushing stdout with fflush(stdout); before calling an input function.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    33
    Uhh, I'm totally flabbergasted by your answer. What is stdout? I don't have any stdout in my code. What do you mean by flushing? I have never heard of the function fflush. Should I just type that before my printf? More help would be appreciated.

  4. #4
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    Maybe a simple google search

    stdout - C++ Reference

    fflush() - C++ Reference

    And, even if you don't type "stdout" anywhere in your code, it is there by default.

    Have a look printf() vs fprintf()
    Last edited by fnoyan; 11-26-2012 at 07:15 PM.

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by Kurtanius21 View Post
    Uhh, I'm totally flabbergasted by your answer. What is stdout? I don't have any stdout in my code. What do you mean by flushing? I have never heard of the function fflush. Should I just type that before my printf? More help would be appreciated.
    stdout, like stdin, is included with stdio.h and is short for "standard output"

    Think of stdout as the outbox for your email. (Using Microsoft's Outlook as an example) You put your emails in your outbox. If you are working offline, it will remain there. You may need to send manually using the Send/Receive option.

    stdout is the outbox for a stream - In your case, to print characters onto your screen Printf puts the characters onto your stdout after processing all the inputs you have given it (like %s or %d).

    By using the command fflush(stdout), you are doing something that is not unlike the Send/Receive button in Microsoft Outlook.


    Have a look at Prelude's response here:
    fflush(stdout)
    Fact - Beethoven wrote his first symphony in C

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by fnoyan
    And, even if you don't type "stdout" anywhere in your code, it is there by default.
    It's not there by default - It is included in stdio.h
    Fact - Beethoven wrote his first symphony in C

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    33
    So now I've got a new problem. I've adjusted my code according to a FAQ. It is now
    Code:
    void find_subject( int **crns, char ***subjects, char ***courses,
         int *count )
    {
      char input[4], str[4];
      int i, j;
    
      while (( i = getchar()) != '\n' && i != EOF);
        printf("Please enter a subject: ");
      if (fgets(input, 4, stdin))
      {
        for ( j = 0 ; j <= *count ; j++ )
        {
          strcpy(str, (*subjects)[j] );
          if ( str == input )
            printf("%i %s %s\n", (*crns)[j], (*subjects)[j], (*courses)[j]);
        }
      }
      printf("\n");
    }
    The problem is, I have an infinite loop. gdb shows that thing cycles endlessly between the for loop and strcpy. It never reaches the if statement. Why?

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    33
    Also, flush isnt working for me. I get the same old problem if I enter the code. Endless prompts and never a chance to enter the subject.
    Code:
    printf("Please enter a subject: ");
        fflush (stdout);
        fgets( input, 4, stdin );
    I'm using Unix if the information matters.

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Code:
    if ( str == input )
    You need to use strcmp to compare strings.

    It might be a good idea to use strncmp and change the '\n' character in the input to '\0' first.
    Fact - Beethoven wrote his first symphony in C

  10. #10
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Try using puts to output your instruction strings.
    Fact - Beethoven wrote his first symphony in C

  11. #11
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    Quote Originally Posted by Click_here View Post
    It's not there by default - It is included in stdio.h
    Well, I meant in printf().... I should have explicitly indicated actually...

  12. #12
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by fnoyan View Post
    Well, I meant in printf().... I should have explicitly indicated actually...
    You also don't have printf without stdio.h
    Fact - Beethoven wrote his first symphony in C

  13. #13
    Registered User
    Join Date
    Oct 2012
    Posts
    33
    That did it! Thank you click here!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets reading remains from stdin
    By koplersky in forum C Programming
    Replies: 6
    Last Post: 11-26-2012, 11:12 PM
  2. fgets with newline in the middle of the stdin
    By sebby64 in forum C Programming
    Replies: 2
    Last Post: 11-27-2010, 01:27 PM
  3. fgets() from stdin and strcat()
    By rocketman03 in forum C Programming
    Replies: 9
    Last Post: 11-01-2008, 01:31 PM
  4. fgets() for stdin not working
    By Yasir_Malik in forum C Programming
    Replies: 2
    Last Post: 03-05-2005, 12:12 PM
  5. using stdin in fgets
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 04-14-2002, 09:14 PM