Re: When to use "perror" and "fprintf"
From: Dan Pop (Dan.Pop_at_cern.ch)
Date: 05/11/04
- Next message: Neil Cerutti: "Re: Do I understand pointers?"
- Previous message: Case: "Re: volatile struct in dot h vs dot c"
- In reply to:(deleted message) Lars Wirzenius: "Re: When to use "perror" and "fprintf""
- Next in thread: Karthik: "Re: When to use "perror" and "fprintf""
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 11 May 2004 14:52:13 GMT
In <kcQlc.185$WS3.72@reader1.news.jippii.net> Lars Wirzenius <liw@iki.fi> writes:
>Darrell Grainger <darrell@NOMORESPAMcs.utoronto.ca.com>:
>> For example, strtol will return LONG_MAX or LONG_MIN if a string is out of
>> range and set errno to ERANGE. So if strtol fails due to out of range, I
>> would use perror.
>
>I have found that it is useful, in practice, to include the value of
>errno in the output. These days, the error message output by perror
>is often translated. This is good for the user, as they get an error
>in a language they understand, but bad for me, if it is in a language
>I do not understand. Therefore, I tend not to use perror and instead
>use something like this:
>
> fprintf(stderr, "Error: Can't open file: %d: %s\n",
> errno, strerror(errno));
>
>If there is an error, I can look up the reason using the numerical
>error code regardless of what the text looks like.
This doesn't work for portable code, as the user's implementation need not
use the same errno codes as yours. Call strerror(errno) twice, once in
the user's locale, the other time in the "C" locale and compare the
two strings. If they're identical, display only one, otherwise display
both and ask the user to quote the English one verbatim.
Dan
-- Dan Pop DESY Zeuthen, RZ group Email: Dan.Pop@ifh.de
- Next message: Neil Cerutti: "Re: Do I understand pointers?"
- Previous message: Case: "Re: volatile struct in dot h vs dot c"
- In reply to:(deleted message) Lars Wirzenius: "Re: When to use "perror" and "fprintf""
- Next in thread: Karthik: "Re: When to use "perror" and "fprintf""
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|