cdev-null

Is /dev/null always openable?


I want to suppress certain fprintf calls by redirecting the destination FILE to /dev/null. But can I be sure, that fopen("/dev/null", "w"); NEVER returns NULL. In other words, is it everytime possible to open this "file"?

If so, I could use this nice ternary operator:

FILE *whereToPrint = (strcmp(custom_topic, ERROR_TOPIC) == 0) ? fopen("/dev/null", "w") : stdout;

fprintf(whereToPrint, "Message sent!\n\n");

Solution

  • Yes, it is possible that fopen fails to open /dev/null for some reason, e.g. permission issue on /dev/null. Though it is very rare but cannot deny the possibility.