Afficher un message
Vieux 11/04/2008, 22h22   #3
Eric Sosman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What is wrong in this program ?

William Pursell wrote:
> [...]
> Or, if you really feel it necessary to be
> excessively verbose:
>
> fprintf( stderr, "%s can't be opened:", filename );
> perror( NULL );


Don't do it that way, because perror() reports the
error that errno indicates, and fprintf() might change
errno. You could wind up with

flatcat.dat can't be opened: not a typewriter

(See Question 12.24 in the FAQ.)

One way to deal with this is to save and restore errno's
value around the fprintf() call:

#include <errno.h>
...
{
int errno_save = errno;
fprintf(stderr, "%s can't be opened: ", filename);
errno = errno_save;
perror(NULL);
...

.... but in a case like this the advice to use

> (or "%s can't be opened: %s", filename, strerror( errno )


.... seems better.

--
Eric.Sosman@sun.com
  Réponse avec citation
 
Page generated in 0,05814 seconds with 9 queries