title: How to write a file with C in Linux?
body: <p>I want to rewrite the "cp" command of Linux. So this program will work like <code>#./a.out originalfile copiedfile</code>. I can open the file, create new file but can't write the new file. Nothing is written. What could be the reason?</p>
  
  <p>The current C code is:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;unistd.h&gt;
  
  int main(int argc,char *aa[]){
      int fd,fd1;
      char buffer[100];
  
      if(argc!=3){
          printf("Usage : ./a.out &lt;original&gt; &lt;copy&gt; \n");
          return -1;
      }
  
      fd=open(aa[1],O_RDONLY,S_IRUSR);
      if(fd==-1){
          printf("file not found.\n");
          return -1;
      }
      fd1=open(aa[2],O_CREAT | O_WRONLY,S_IRUSR);
      if(fd1!=-1){
          printf("file is created.\n");
      }
      ssize_t n;
      while(n=read(fd,buffer,50)){
          write(fd1,buffer,n);
          printf("..writing..\n");
      }
      close(fd);
      close(fd1);
  }
  </code></pre>