Mail Archives: djgpp/2001/09/14/12:24:05
> From: "Ben Davis" <ben AT vjpoole DOT freeserve DOT co DOT uk>
> Newsgroups: comp.os.msdos.djgpp
> Date: Fri, 14 Sep 2001 11:27:15 +0100
>
> I wrote a program which reads a large quantity of data from one file and
> then writes data to another. The output file is chosen using the tmpnam()
> function, to be renamed at the end. It is most convenient for me to detect
> errors by checking if errno is nonzero once at the end of the program. To my
> dismay, I found ENOENT in the variable by the time my program had finished -
> and this is after the input file had been opened, since an error at that
> point would have been output with a different message.
As others told you, the value of `errno' should only be tested in the
case of failure. And even then, you should zero the value of errno
before the call to a library function and test it after the function
returns a failure indication.
The `tempnam' function is documented to retun NULL if it fails to
generate a valid file name. So you should test that instead.
> However, it is my understanding that errno should be preserved by
> all successful library calls.
No, this isn't so. Any system call that fails changes errno, and a
successful library function doesn't bother to set errno to the
previous value, since no one is supposed to look at errno in that
case.
- Raw text -