title: C write() function not working
body: <p>I am trying to write into a file, but it is not working. I can open a file, but while writing in the file using write function, tt is writting in the stdout itself, and the content of the file I opened remain unchanged.</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;sys/file.h&gt;
  #include&lt;sys/types.h&gt;
  #include&lt;sys/stat.h&gt;
  #include&lt;limits.h&gt;
  #include&lt;fcntl.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;sys/uio.h&gt; 
  main() {  
    char fn[30]; 
    int fd,i=0; 
    int actualbytes,bytesstored; 
    char buffer[100];
  
    printf("\nEnter the filename with path"); 
    scanf("%s",fn); 
  
    if(fd=open(fn,O_WRONLY|O_CREAT,S_IWUSR|S_IWUSR)&lt;0) 
    {  
       perror("open"); 
       exit(0); 
    } 
    else 
    {
      write(stdout,"\n\nEnter the contents for the file\n");  
      write(stdout,"press CTRl+D at the end of the file\n\n");
  
      fflush(stdout); 
      while((buffer[i]=getc(stdin))!=EOF)  i++;
  
      buffer[i]='\0'; 
      bytesstored=sizeof(buffer); 
  
      if(actualbytes=write(fd,buffer,bytesstored)&lt;0)
      {  
         perror("write");  
         exit(0); 
      } 
      else 
      { 
         write(stdout,"\n\nfile is opened successfully");
  
         write(stdout,"\nThe contents are written"); fflush(stdout); 
      } 
      if(close(fd)&lt;0) 
      {  
         perror("close");  
         exit(0); 
      } 
      else
         printf("\nfile is closed"); 
   } 
  }
  </code></pre>