title: Why does open() create my file with the wrong permissions?
body: <p>I am trying to read some text from a file and write it to another using <code>open()</code>, <code>read()</code> and <code>write()</code>.</p>
  
  <p>This is my <code>open()</code> for the file-to-write-to (I want to create a new file and write into it):</p>
  
  <pre><code>fOut = open ("test-1", O_RDWR | O_CREAT | O_SYNC);
  </code></pre>
  
  <p>This is setting file-permissions to something I don't understand at all. This is the output of <code>ls -l</code>:</p>
  
  <pre><code>---------T 1 chaitanya chaitanya 0 2010-02-11 09:38 test-1
  </code></pre>
  
  <p>Even the read permission is locked. I tried searching for this, but could not find ANYTHING.
  Strangely, <code>write()</code> still successfully writes data to the file.</p>
  
  <p>Also, if I do a 'chmod 777 test-1', things start working properly again.</p>
  
  <p>Could someone please let me know where I am going wrong in my open call?</p>
  
  <p>Thanks!</p>
  
  <p>For your reference, I have pasted the complete program below:</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 () {
  
      char buffer[512], ch;
  
      int fIn, fOut, i;
      ssize_t bytes;
      FILE *fp = NULL;
  
      //open a file
      fIn = open ("test", O_RDONLY);
      if (fIn == -1) {
          printf("\nfailed to open file.");
          return 1;
      }
  
      //read from file
      bytes =  read (fIn, buffer, sizeof(buffer));
      //and close it
      close (fIn);
  
      printf("\nSuccessfully read %d bytes.\n", bytes);
  
      //Create a new file
      fOut = open ("test-1", O_RDWR | O_CREAT | O_SYNC);
  
      printf("\nThese are the permissions for test-1\n");
      fflush(stdout);
      system("ls -l test-1");
  
      //write to it and close it.
      write (fOut, buffer, bytes);
      close (fOut);
  
  
      //write is somehow locking even the read permission to the file. Change it.
      system("chmod 777 test-1");
  
      fp = fopen ("test-1", "r");
      if (fp == NULL) {
          printf("\nCan't open test-1");
          return 1;
      }
  
      while (1)
      {
          ch = fgetc(fp);
          if (ch == EOF)
              break;
          printf("\n%c", ch);
      }
  
      fclose (fp);
  
      return 0;
  }
  </code></pre>