Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1984
  • Last Modified:

stdout and stderr in Linux

UNIXer,

Can I redirect the stdout and stderr to a temporary file by the following code ?
  filename=tmpnam(0);
  freopen(filename, "w", stderr);
  freopen(filename, "w", stdout);

But, how come I cannot print characters to the original stdout and stderr by doing the following code ?
  stdout=fopen("/dev/stdout", "w");
  stderr=fopen("/dev/stderr", "w");

Anyway, how can I achieve it ?

Andrew
0
andrewyu
Asked:
andrewyu
  • 3
1 Solution
 
seedyCommented:
By default stdin and stdout will be pointing to the terminal.
So, when you want to restore them, you would do
   stdout = fopen("/dev/tty", "w");
   stderr = fopen("/dev/tty", "w");
Hope this helps.
PS: Andrewyu, why is that you never grade answers?
   
0
 
seedyCommented:
BTW, there is no device like '/dev/stdout' or '/dev/stderr'.
From man pages for 'tty':
The file '/dev/tty' is, in each process, a synonym for the
control terminal associated with the process group of that
process, if any.
0
 
sinuCommented:
Go thru a good book on unix file io.

 there is no device like '/dev/stdout' or '/dev/stderr'.
From man pages for 'tty':
The file '/dev/tty' is, in each process, a synonym for the
control terminal associated with the process group of that
process, if any.
0
 
seedyCommented:
Sinu,
Hope you did not mean to do that!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now