DeathKing
DeathKing

Reputation: 173

C: Permission Denied with open() (GCC for windows)

So, I just write:

int fdes = open(path, O_WRONLY | O_CREAT | O_TRUNC);

And if the file that the path referred to doesn't exist, this code works. But if it does, the code returns with errno 13 : Permission Denied. I never use the O_EXCL mode at all.

I have searched for the solution to this problem a long time and I am really confused. Can you explain the problem?

I use the GCC for windows(4.5.2). Your answer will be appreciated.

Upvotes: 5

Views: 5339

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 753665

  1. If you use O_CREAT, you should supply a mode as the third argument to open().
  2. If the file does exist, you must have write permission on the file. The error indicates (strongly suggests) that the file is created without write permission. This might be a consequence of not creating it with a sensible mode.

Note that (on Unix at least) you can create a file for writing with mode 0444 (or even 0); the permissions affect everyone else, but not the process that created the file while it uses the file descriptor that created the file.

Upvotes: 5

Related Questions