title: Redirect stdout and stderr to the same file and restore it
body: <p>I am redirecting the output of stderr and stdout of my c program to two files and then restoring the original stdout and stderr:</p>
  
  <pre><code>int sout = dup(fileno(stdout));
  freopen("test.txt","w",stdout);
  
  int serr = dup(fileno(stderr));
  freopen("test.txt","a",stderr);
  
  //some output....
  
  dup2(sout,fileno(stdout));
  close(sout);
  
  dup2(serr,fileno(stderr));
  close(serr);
  </code></pre>
  
  <p>That's the code axample. This works. </p>
  
  <p>But I would like to redirect stdout and stderr to the same file(and later restore it again) so that the output is sorted in the same order as it is sorted on the console output when not redirecting stderr and stdout. How can I do that?</p>