04-19-04 11:34 PM
In article <40844372.8010408@removethisfoglight.com>,
Fletcher Glenn <fletcher@removethisfoglight.com> wrote:
> Ole Jacob Hagen wrote:
>
> perror or strerror will not produce meaningful errors unless read
> returns -1. If you are arbitrarily printing errors without checking
> for -1, you can get leftover values in errno.
Another common mistake is to call some other functions in between
calling read() and calling perror(), e.g.
nread = read(...);
if (nread == -1) {
printf("read() returned an error.\n");
perror(...);
}
The problem with this is that the functions that printf() calls
internally may modify errno. So you end up printing the message that
goes with that value, rather than the errno value that was set by
read(). If you must call other functions before calling perror(), you
have to save the original error; you can do this either by saving and
restoring the value of errno, or by calling strerror() to save the error
message string.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
[ Post a follow-up to this message ]
|