title: I/O redirection in a C program
body: <p>I want to implement a simple "cat file1 > file1" command in a C program. I have tried the following, but it does not work...</p>
  
  <pre><code>main () {
      pid_t pid;
      FILE *ip, *op;
      char *args[3];
      printf("Name of the executable program\n\t");
      scanf("%s", &amp;name[0]); // I entered cat here
      printf("Name of the input file\n\t");
      scanf("%s", &amp;name[1]); //file1.txt
      printf("Name of the output file\n\t");
      scanf("%s", &amp;name[0]); //file2.txt
      pid = fork();
      if(pid == -1)
          perror("fork() error");
      else if(pid &gt; 0)
          waitpid(-1, NULL, 0);
      else if (pid == 0) {
          op = fopen(name[2], "w");
          close(1);
          dup(op);
          execlp(name[0], name[1], NULL);
          }
      return 0;
      }// end of main()
  </code></pre>
  
  <p>I thought the <code>execlp()</code> will run <code>cat file1.txt</code> and its output will be redirected to <code>file2.txt</code>, but it's not and I don't know why. How do I do it?</p>