I mostly agree with the existing answer:
fopen( "/dev/null", "w")
means "open for writing, create if it doesn't exist, then truncate to zero bytes"
By trial and error, I discovered that the problematic flag is O_CREAT
— you can't create /dev/null
if it doesn't exist. Go figure.
Also by trial and error: the following works:
fopen("/dev/null", "r+")
Here, "r+"
means "open for reading and writing", which is a clumsy way to say "the file should exist". You don't need read permissions, so it's less than ideal.
Another way to make this work:
int fd = open("/dev/null", O_WRONLY);
FILE *f = fdopen(fd, "w");
The manual page for fdopen
says
The mode of the stream ... must be compatible with the mode of the file descriptor
which is vague, but of course this has no chance to fail because of O_CREAT
— fdopen
has no access to file name so it can't try to create it.
This looks like a configuration problem on this specific system; fopen
with plain "w"
mode should work. So you shouldn't change the way you usually write code because of this.
fopen
andopen
(one of them being the issue of buffered I/O). Maybe it will give a hint towards an answer. – Eulogium"w"
does not directly correspond toO_WRONLY
, it corresponds toO_WRONLY|O_CREAT|O_TRUNC
. – Mcmillenopen("/dev/null", O_WRONLY | O_TRUNC | O_CREAT, 0666 );
that also fails with "Permission denied". – SpringfieldO_CREAT
flag triggers "Permission denied" – Stonebroke