title: open file in C and overwrite if it exists
body: <p>I am working on a C program. I am not being able to open file and overwrite if it exists. I think the code is right, It is appending instead.</p>
  
  <pre><code>int in, out, append ,pid;
  if (sc-&gt;infile)
  {
      in = 1;
      printf("infile");
  }
  if (sc-&gt;outfile){
      out = 1;
      printf("outfile");
  }
  if (sc-&gt;append)
      append = 1;
  
  
  if ((pid = fork()) &lt; 0)
      perror("some erro");
  else if (pid == 0)
  {
      /* Be childish */
      if (in)
      {
  
          int fd0 = open(sc-&gt;infile, O_RDONLY);
          dup2(fd0, STDIN_FILENO);
          close(fd0);
      }
  
      if (out)
      {
          printf("outfile detece");
          int fd1;
          if (append)
               fd1 = open(sc-&gt;outfile, O_WRONLY | O_CREAT | O_APPEND, 0666);
          else
  
              fd1 = open(sc-&gt;outfile, O_WRONLY | O_CREAT , 0666);;
          dup2(fd1, STDOUT_FILENO);
          close(fd1);
      }
  
      execvp(sc-&gt;argv[sc-&gt;cmdStart[0]], &amp;(sc-&gt;argv[sc-&gt;cmdStart[0]])); 
          fprintf(stderr, "Failed to exec\n" );
      exit(1);
  }
  else
  {
  /* Be parental */
      wait(0);
  }
  </code></pre>
  
  <p>Could someone help on this. I have tried a lot to fix this.</p>