LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 02-21-2005, 05:46 AM   #1
rahul_kulkarni
Member
 
Registered: Dec 2004
Posts: 32

Rep: Reputation: 15
Reopen the stdout and stderr


Hello All,

Is it possible to reopen the stdout and stderr descriptos after closing them.

For e.g.

int main()
{
FILE *ftemp = fopen("/tmp/one.txt", "w+");
int fd = fileno(ftemp);
close(1); /* close the stdout associated with screen */
close(2); /* close the stderr associated with screen */

dup(fd); /* one.txt is new stdout for the process */
dup(fd); /* one.txt is new stderr for the process */

close(1); /* close the one.txt stdout */
close(2); /* close the one.txt stderr */

/*
Now here is it possible to associate and 1 and 2 file descriptors to screen?

*/

return 0;
}


This I need because some part of program output (and error) I want to redirect it to a file and other on a screen.

Thanks,
// Rahul
 
Old 02-21-2005, 05:57 AM   #2
aus9
Guru
 
Registered: Oct 2003
Posts: 5,055

Rep: Reputation: Disabled
I umm only read the last line.....

try just running your command and pipe it to a file at the beginning of the run

eg file to run > /home/yourname/myoutput.txt

> creates file "myoutput.txt"

>> would append to a file if you needed to do several runs?
 
Old 02-21-2005, 06:03 AM   #3
rahul_kulkarni
Member
 
Registered: Dec 2004
Posts: 32

Original Poster
Rep: Reputation: 15
I want to do it from the C program and not by using the redirection operator on command line as I want log some output on the screen and some in the file.

// Rahul
 
Old 02-21-2005, 06:55 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,481

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
open /dev/tty, and it should go to your keyboard/screen
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
redirecting stdout and stderr to a file Avatar33 Programming 4 03-12-2005 07:55 AM
shell short question(stdout, stderr) blackzone Programming 4 11-11-2004 09:01 AM
stderr is not a tty where are you? davidschob Slackware 6 10-31-2003 10:38 AM
TCSH: redirect stdout and stderr seperately ugenn Linux - General 2 06-06-2002 12:07 PM


All times are GMT -5. The time now is 03:57 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration