Why does open() create my file with the wrong permissions?

open() takes a third argument which is the set of permissions, i.e.

open(filename, O_RDWR|O_CREAT, 0666)

0666 is an octal number, i.e. every one of the 6’s corresponds to three permission bits

6 = rw

7 = rwx

first three bits for owner permission, next three bits for group permission and next is for the world
the first digit – represents that is file or directory. (0 – file, d – directory)
here we used 0 means file

It’s a typical pitfall. The compiler allows you to leave the permission argument away because when you open an existing file the permission bits don’t make sense. But when you forget the argument when you create a file, you get a random set of permissions, e.g. 0000 in your case (—).

Leave a Comment