perror

suggest change

To print a user-readable error message to stderr, call perror from <stdio.h>.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
   FILE *fout;
   const char *path = argc > 1 ? argv[1] : "output.txt";

   if ((fout = fopen(path, "w")) == NULL) {
      perror("fopen: Could not open file for writing");
      return EXIT_FAILURE;
   }
   if (fclose(fout) == EOF) {
      perror("fclose");
      return EXIT_FAILURE;
   }
   return EXIT_SUCCESS;
}

This will print an error message concerning the current value of errno.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents