title: C system calls open / read / write / close and O_CREAT|O_EXCL
body: <p>Given the following code (it's supposed to write "helloworld" in a "helloworld" file, and then read the text):</p>
  
  <pre><code>#include &lt;fcntl.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/stat.h&gt;
  
  #define FNAME "helloworld"
  
  int main(){ 
      int filedes, nbytes;
      char buf[128];
  
      /* Creates a file */
      if((filedes=open(FNAME, O_CREAT | O_EXCL | O_WRONLY | O_APPEND, 
          S_IRUSR | S_IWUSR)) == -1){
              write(2, "Error1\n", 7);
      }
  
      /* Writes hello world to file */
      if(write(filedes, FNAME, 10) != 10)
          write(2, "Error2\n", 7);
  
      /* Close file */
      close(filedes);
  
      if((filedes = open(FNAME, O_RDONLY))==-1)
          write(2, "Error3\n", 7);
  
      /* Prints file contents on screen */    
      if((nbytes=read(filedes, buf, 128)) == -1)
          write(2, "Error4\n", 7);
  
      if(write(1, buf, nbytes) != nbytes)
          write(2, "Error5\n", 7);
  
      /* Close file after read */
      close(filedes); 
  
      return (0);
  }
  </code></pre>
  
  <p>The first time I run the program, the output is:</p>
  
  <pre><code>helloworld
  </code></pre>
  
  <p>After that every time I to run the program, the output is:</p>
  
  <pre><code>Error1
  Error2
  helloworld
  </code></pre>
  
  <p>I don't understand why the text isn't appended, as I've specified the O_APPEND file.
  Is it because I've included O_CREAT? 
  It the file is already created, shouldn't O_CREAT be ignored?</p>