GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Mar-2004, 14:24
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,328
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Pipes and Redirection


I just posted a GID Community blog entitled: Piping and Redirection.

Quote:
There are three predefined devices:

stdin -- standard input read more...

Please let me know what you think.

This is an auto-generated message posted by GIDNetwork™ on behalf of the author.
Last edited by admin : 22-Aug-2005 at 15:58.
  #2  
Old 11-Apr-2004, 08:01
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Stderr Redirection


This is a small addition to this excellent piping and redirection tutorial. This is not so much a C/C++ theme as it is a scripting theme.

It is also possible to redirect the stderr stream as well as the stdin stream. This could be useful, if you want to create a "log" file of your errors/warnings, but not have them interfere with the "normal" output of your program.

Take this small, worthless example of a c code:
CPP / C++ / C Code:
#include <stdio.h>

int main(){
	fprintf(stderr,"This goes to the stderr location.");
	printf("This is the standard output.");
	
	return 0;
}

If you compile and run this, you will get:
Code:
This goes to the stderr location. This is the standard output.

However, if you run this program with a redirection like:
Code:
pipetest 2>logfile.txt

You will get on the console only:
Code:
This is the standard output.
and you will also get a text file named logfile.txt that will contain the stderr output.

If you run it again with:
Code:
pipetest 2>>logfile.txt
It will add the stderr output to the end of the "log" file, so that you will now get two lines of the stderr output.

This is quite helpful in writing daemon routines. For *nix only, I could make any program a daemon by using:
Code:
mydaemon 1>>/var/log/messages 2>mydaemon.log &

The ampersand tells *nix to spawn the process in the background and return control to the calling program. This would throw all standard output to the end of the existing /var/log/messages file and throw all of the standard error stuff to a newly created file called mydaemon.log.
 
 

Recent GIDBlogGang violence and the drug culture by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:36.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.