Example 1 — Report why a file could not be opened
The message text for ENOENT is the usual "No such file or directory".
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main(void) {
const char *path = "no_such_file.txt";
FILE *fp = fopen(path, "r");
if (!fp) {
printf("open failed: %s\n", strerror(errno));
return 1;
}
fclose(fp);
return 0;
} How It Works
fopenreturnsNULLand setserrnotoENOENT.strerror(errno)converts the code to text.- The message wording is defined by the C library and may differ slightly between platforms.
