title: C Program that makes a copy of a file using standard I/O and system calls
body: <p>I am trying to write a C program which uses standard I/O and System calls to perform copying of contents of one file to another file.</p>
  
  <p>So far, I have done this :</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;fcntl.h&gt;
  
  int main(int argc, char *argv[])
  {
      int fd1, fd2;
      char buffer[1024];
      long int n1;
  
      if(((fd1 = open(argv[1], O_RDONLY)) == -1) || ((fd2=open(argv[2],O_CREAT|O_WRONLY|O_TRUNC, 0700)) == -1)){
          perror("file problem");
          exit(1);
      }
  
      while((n1=read(fd1, buffer, 1024) &gt; 0)){
          if(write(fd2, buffer, n1) != n1){
              perror("writing problem ");
              exit(3);
          }
      }
      close(fd1);
      close(fd2);
  }
  </code></pre>
  
  <p>When I run the program like this : </p>
  
  <pre><code>cc copyContents.c 
  ./a.out one.txt two.txt
  </code></pre>
  
  <p>Assuming that <code>one.txt</code> is well defined, what I want is to create a new file called <code>two.txt</code> and copy over all the contents of <code>one.txt</code></p>
  
  <p>When I look into the contents of <code>two.txt</code> after running the program, it has literally nothing in it. Just a blank file.</p>
  
  <p>Where am I going wrong?</p>