title: Unexpected fgets behavior on EOF
body: <p>I have this code that gets a line from stdin:</p>
  
  <pre><code>char string[USR_SIZE+1];
  /*USR_SIZE is 9 */ 
  fgets(string, USR_SIZE, stdin);
  size_t len = strlen(string);
  if (string[len-1] == '\n') {
      // read in whole line, no need to clear buffer
      string[len-1] = 0;
  }    
  else {
      if(feof(stdin)){
          printf("EXACT LEN\n");
      }
      else{
          //username too long, clear stdin and retry
          printf("Dumping...");
          dump_line(stdin); //clear stdin
          /*something that loops until success*/
  
      }
  
  }
  </code></pre>
  
  <p>No newline in the buffer <code>string</code> means either <code>EOF</code> or that the string was cut. </p>
  
  <p>Now. The optimal string needed is like <code>"user_123"</code> (8 char). Now if I type <code>"user_12345678"</code> it correctly stores only <code>"user_123"</code> and executes the else body. If I type <code>"user_123"</code> it executes else body too. </p>
  
  <p>I want to distinguish a <code>"user_123"</code> (<code>EOF</code>) string from a <code>"user_1234"</code> (cutted) because of only in this last case input is not correct. Why <code>feof</code> is not set ?</p>
  
  <p>Furthermore: how i can check <code>fgets</code> return value for error managament?</p>
  
  <p>Found this solution:</p>
  
  <pre><code>if (fgets(string, USR_SIZE + 1, stdin)){
      if (strlen(string) == USR_SIZE &amp;&amp; string[USR_SIZE - 1] == '\n'){
          printf("EXACT\n");
          /* User entered an eight character string. */
          string[USR_SIZE - 1] = 0;
      }
      else{
          if(strlen(string) &lt; USR_SIZE-1){
          printf("INVALID&lt;&lt;&lt;\n");
          /*don't dump*/
      }
          else{
              printf("INVALID&gt;&gt;&gt;\n");
              dump_line(stdin);
          }
      }
  
      printf("EXIT\n");
      printf("%s\n", string);
  }
  else{
      free(auth_data);
      EXIT_ON_ERROR_("Error while getting user input\n");
  }
  </code></pre>