473,396 Members | 1,898 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

When to use "perror" and "fprintf"

Reading the man pages and some code did not really help me in
understanding the difference between - or better when I should use -
perror("...") and fprintf(stderr, "...")

Any hint or help is appreciated.

L. Westmeier

Nov 14 '05 #1
5 12441
"L. Westmeier" <we*******@informatik.hu-berlin.de> wrote:
Reading the man pages and some code did not really help me in
understanding the difference between - or better when I should use -
perror("...") and fprintf(stderr, "...")


They do rather different things. You use perror() to print a message to
stderr that corresponds to errno. You use fprintf() to print _anything_
to stderr, or any other stream.
perror() is a very specialised printing function;

perror(str);

is equivalent to

if (str)
fprintf(stderr, "%s: %s\n", str, strerror(errno));
else
fprintf(stderr, "%s\n", strerror(errno));

Richard
Nov 14 '05 #2
On Tue, 4 May 2004, L. Westmeier wrote:
Reading the man pages and some code did not really help me in
understanding the difference between - or better when I should use -
perror("...") and fprintf(stderr, "...")


The perror function works differently depending on the value of errno. If
you use a function that effects errno then it makes sense to use perror.
If you use a function that does not effect errno and simply returns an
error code you should use fprintf(stderr, fmt, ...).

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.

--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@whitehouse.gov
Nov 14 '05 #3
L. Westmeier wrote:
Reading the man pages and some code did not really help me in
understanding the difference between - or better when I should use -
perror("...") and fprintf(stderr, "...")

Any hint or help is appreciated.

L. Westmeier

They are entirely different things. No need for confusion whatsoever.

fprintf - can be used to print to any stream.

perror - prints the error message string, if errno is set. (Usually
best done after a sys. call ).

For eg, if you fail to open a directory using fopen , errno is set to
EISDIR say.
sometimes it would make much more sense to print the corresponding
error message than playing with a macro like EISDIR (to the user, of
course !!).

--
Karthik.
Humans please 'removeme_' for my real email.
Nov 14 '05 #4
In <kc**************@reader1.news.jippii.net> Lars Wirzenius <li*@iki.fi> writes:
Darrell Grainger <da*****@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: Da*****@ifh.de
Nov 14 '05 #5
HI,

I use the function 'fprintf' to show the 'Usage' message.
if(argc != 2){
fprintf(stderr, "Usage: %s [filename]\n", argv[0]);
exit(1);
}
perror() cannot do that.

When I have to show the standard error message and
more information, fprintf() is very useful for me!

"L. Westmeier" <we*******@informatik.hu-berlin.de> wrote in message news:<c7**********@hahn.informatik.hu-berlin.de>...
Reading the man pages and some code did not really help me in
understanding the difference between - or better when I should use -
perror("...") and fprintf(stderr, "...")

Any hint or help is appreciated.

L. Westmeier

Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: Adam | last post by:
Hi, Does anyone know of an online C-to-HTML converter to display code nicely formatted in a browser? Thanks a lot, Adam
8
by: VijaKhara | last post by:
Hi all, Please tell me how to create a file (using fopen) in a sub-folder (such as sub-folder "Mygoal")? I tried if ( ( fp = fopen ( "mygoal\myfile.tif", "wb" ) ) == NULL ) { fprintf (...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.