LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Reopen the stdout and stderr (https://www.linuxquestions.org/questions/programming-9/reopen-the-stdout-and-stderr-292914/)

rahul_kulkarni 02-21-2005 05:46 AM

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

aus9 02-21-2005 05:57 AM

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?

rahul_kulkarni 02-21-2005 06:03 AM

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

jlliagre 02-21-2005 06:55 AM

open /dev/tty, and it should go to your keyboard/screen


All times are GMT -5. The time now is 04:33 PM.