In article <news:ef**************************@posting.google. com>
Karthick S. <ka*******@spymac.com> wrote:
[snippage, and edited-for-space-reasons]
if(fgets(*r_pstrState, 5, fp) == NULL) {
sprintf(strErrMesg,
"Reading from the file %s failed. Error: %s",
CURRSTATECONF, strerror(errno));
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.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it
http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.