LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-29-2002, 05:46 PM   #1
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Rep: Reputation: 30
question about printing to stderr in C


Hi. What is the difference in using >

fprintf (stderr, "Error reading file" );
exit(1);

when dealing with error checking while opening a file?
as opposed to using >

printf ("Error reading file");
exit(1);

wouldn't they both accomplish the same thing (printing an error to
standard output)?
I can see that in the 'fprintf' something is sent to stderr. But what is sent to stderr? exit(1) ?

Just basically want to know the advantage of using the fprintf as opposed to just printing "Error reading file" and sending an 'exit(1)'.

thanks

Last edited by purpleburple; 10-29-2002 at 05:48 PM.
 
Old 10-29-2002, 05:54 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
well stderr means Standard Error. it's a stream where error messages go, as opposed to stdout - Standard Out where program output goes. if you are writing a program that reads output from another for example, you don't want to necessarily be given error messages as your input. you only want the data you want. by having seperate streams you can easily sort output types out.

try.....

ls file_that_doesnt_exist file_that_does_exist > file_out 2> file_err

now, file_err should contain an error message saying the first file doesn't exist, while file_out contains standard ls output for the second file. This is because the two types of output are placed in different streams, and in our command we split that stream up ( > redirects stdout, 2> redirects stderr). you might not think it's hugely useful right now (although i hope you can see the potential benefits) but when you get more involved, these things become very important.

Last edited by acid_kewpie; 10-29-2002 at 05:55 PM.
 
Old 10-30-2002, 01:37 PM   #3
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Original Poster
Rep: Reputation: 30
thank you very much acid!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
stderr messages bokini Linux - General 1 11-29-2005 07:30 AM
shell short question(stdout, stderr) blackzone Programming 4 11-11-2004 09:01 AM
stderr redirection evilop Linux - Newbie 3 09-21-2004 04:27 PM
redirecting stderr nodger Programming 2 03-11-2004 10:58 AM
stderr is not a tty where are you? davidschob Slackware 6 10-31-2003 10:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration