title: Error 13 (Permission Denied) when running open on file I created (iOS)
body: <p>I have a simple program running on iOS (iPad) which opens a file, writes some data, closes the file, and then tries to reopen the file. The strange thing is that the second open sometimes fails with return code -1 and errno 13 (Permission Denied). If I run the code several times it will alternately fail and succeed (the pattern seems random).</p>
  
  <p>Below is the code I am using, can anyone explain what I am doing wrong? The first open always succeeds and the write also always succeeds. </p>
  
  <pre><code>NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];
  NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"file.txt"];
  
  result = remove([appFile cStringUsingEncoding:NSASCIIStringEncoding]);
  
  int fp = open([appFile cStringUsingEncoding:NSASCIIStringEncoding], O_RDWR | O_CREAT);
  result = write(fp, "abc", 3);
  close(fp);
  
  fp = open([appFile cStringUsingEncoding:NSASCIIStringEncoding], O_RDONLY);
  NSLog(@"fp = %i, errno = %i", fp, errno);
  close(fp);
  </code></pre>