title: redirecting standard output in c then resetting standard output
body: <p>I'm trying to use redirects in C to redirect input to one file and then set standard output back to print to the screen. Could someone tell me what's wrong with this code?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;unistd.h&gt;
  
  int main(int argc, char** argv) {
      //create file "test" if it doesn't exist and open for writing setting permissions to 777
      int file = open("test", O_CREAT | O_WRONLY, 0777);
      //create another file handle for output
      int current_out = dup(1);
  
      printf("this will be printed to the screen\n");
  
      if(dup2(file, 1) &lt; 0) {
          fprintf(stderr, "couldn't redirect output\n");
          return 1;
      }
  
      printf("this will be printed to the file\n");
  
      if(dup2(current_out, file) &lt; 0) {
          fprintf(stderr, "couldn't reset output\n");
          return 1;
      }
  
      printf("and this will be printed to the screen again\n");
  
      return 0;
  }
  </code></pre>