title: Return value of fgets()
body: <p>I have just recently started working with <code>I/O</code> in <code>C</code>. Here is my question - <br />I have a file, from which I read my input. Then I use <code>fgets()</code> to get strings in a buffer which I utilise in some way. Now, what happens if the input is too short for the buffer i.e. if the first read by <code>fgets()</code> reaches <code>EOF</code>. Should <code>fgets()</code> return <code>NULL</code>(as I have read in <code>fgets()</code> documentation)? It seems that it doesn't and I get my input properly. Besides even my <code>feof(input)</code> does not say that we have reached <code>EOF</code>. <br />Here is my code snippet.</p>
  
  <pre><code>char    buf[BUFSIZ];
  FILE    *input,
          *output;
  
  input   = fopen(argv[--argc], "r");
  output  = fopen(argv[--argc], "w");
  
  /**
   *  If either of the input or output were unable to be opened
   *          we exit
   */
  if (input == NULL) {
      fprintf(stdout, "Failed to open file - %s.\n", argv[argc + 1]);
      exit(EXIT_FAILURE);
  }
  
  if (output == NULL) {
      fprintf(stdout, "Failed to open file - %s.\n", argv[argc + 0]);
      exit(EXIT_FAILURE);
  }
  
  if (fgets(buf, sizeof(buf), input) != NULL) {
      ....
  }
  
  /**
   *  After the fgets() condition exits it is because, either -
   *      1) The EOF was reached.
   *      2) There is a read error.
   */
  if (feof(input)) {
      fprintf(stdout, "Reached EOF.\n");
  }
  else if (ferror(input)) {
      fprintf(stdout, "Error while reading the file.\n");
  }
  </code></pre>