You asked:
Is it something in the C standard that prevents fgetc
to return EOF
if end-of-file has not been attained?
On the contrary, the standard explicitly allows EOF
to be returned when an error occurs.
If a read error occurs, the error indicator for the stream is set and the fgetc
function returns EOF
.
In the footnotes, I see:
An end-of-file and a read error can be distinguished by use of the feof
and ferror
functions.
You also asked:
Or is the if ((c = fgetc (stream)) != EOF)
syntax not fully portable?
On the theoretical platform where CHAR_BIT
is more than 8 and sizeof(int) == 1
, that won't be a valid way to check that end-of-file has been reached. For that, you'll have to resort to feof
and ferror
.
c = fgetc (stream);
if ( !feof(stream) && !ferror(stream) )
{
// Got valid input in c.
}