Mac said:
I may not have expressed myself as clearly as I could have. The
bottom line, though, is that fgets will not return NULL if it
reads a few characters and THEN encounters eof. It will return
NULL when it encounters the end of the file without reading any
characters into the buffer. Unless I am reading the standard
wrong, which is certainly possible.
The following covers fgets(). There is an additional provision
somewhere that states that action on final file lines without a
\n termination is undefined or implementation defined.
From N869:
7.19.7.2 The fgets function
Synopsis
[#1]
#include <stdio.h>
char *fgets(char * restrict s, int n,
FILE * restrict stream);
Description
[#2] The fgets function reads at most one less than the
number of characters specified by n from the stream pointed
to by stream into the array pointed to by s. No additional
characters are read after a new-line character (which is
retained) or after end-of-file. A null character is written
immediately after the last character read into the array.
Returns
[#3] The fgets function returns s if successful. If end-of-
file is encountered and no characters have been read into
the array, the contents of the array remain unchanged and a
null pointer is returned. If a read error occurs during the
operation, the array contents are indeterminate and a null
pointer is returned.
____________________
232An end-of-file and a read error can be distinguished by
use of the feof and ferror functions.