if(fgets(*r_pstrState, 5, fp) == NULL) {
sprintf(strErrMesg,
"Reading from the file %s failed. Error: %s",
CURRSTATECONF, strerror(errno));[/QUOTE]
This is not a good idea. The fgets() function is never guaranteed
to set errno to anything, even on error. In practice, on real
operating systems, reading from a file does tend to set errno on
error, but not on EOF.
[at EOF] I get the error message:
Reading from the file <path to file> failed. Error: Success.
This is quite unsurprising.
Do not inspect errno after fgets() fails. If you want to know
*why* fgets() has failed, use feof() and ferror() to distinguish
between "fgets() failed because of end-of-file" and "fgets() failed
because of error reading file".
Note that you should not use feof() *before* getting an input
failure of some sort, because feof() does not predict input failure
due to end of file, but rather "postdict" it: *after* failure, one
of feof() or ferror() should be true.
Note again that Standard C does not guarantee anything useful
in errno even in the ferror() case, so something like:
if (fgets(*r_pstrState, 5, fp) == NULL) {
if (feof(fp)) {
... normal end of file case ...
} else {
sprintf(strErrMesg,
"Reading from the file %s failed. The system "
"most recently reported \"%s\", which may or "
"may not have anything to do with the failure.",
CURRSTATECONF, strerror(errno));
...
}
}
will include the (potential) errno-based information along with a
caveat.