Interview answers verified by specialists.

Find interview questions and answers on this website:

See right interview answers on 30 common job interview questions

How can you restore a redirected standard stream?

The preceding example showed how you can redirect a standard stream from within your program. But what
if later in your program you wanted to restore the standard stream to its original state? By using the standard
C library functions named dup() and fdopen(), you can restore a standard stream such as stdout to its
original state.

The  dup() function duplicates a file handle. You can use the  dup() function to save the file handle
corresponding to the  stdout standard stream. The  fdopen() function opens a stream that has been
duplicated with the dup() function. Thus, as shown in the following example, you can redirect standard
streams and restore them:

#include <stdio.h>
void main(void);

void main(void)
     int orig_stdout;
     /* Duplicate the stdout file handle and store it in orig_stdout. */
     orig_stdout = dup(fileno(stdout));
     /* This text appears on-screen. */
     printf("Writing to original stdout...\n");
     /* Reopen stdout and redirect it to the "redir.txt" file. */
     freopen("redir.txt", "w", stdout);
     /* This text appears in the "redir.txt" file. */
     printf("Writing to redirected stdout...\n");
     /* Close the redirected stdout. */
     /* Restore the original stdout and print to the screen again. */
     fdopen(orig_stdout, "w");
     printf("I'm back writing to the original stdout.\n");

Do you know that?

23% jobs posted on Elance are for Design & Multimedia Next


Getting payments for Freelancers
1. Read how to register and get earned money

2. Open account within 5 minPayoneer sing up to get free $25