Page 157 - C-Language
P. 157

}

             /* do some processing and try opening the file differently, then */


             if (last_error) {
                 fprintf(stderr, "fopen: Could not open %s for writing: %s",
                         argv[1], strerror(last_error));
                 fputs("Cross fingers and continue", stderr);
             }

             /* do some other processing */

             return EXIT_SUCCESS;
         }


        perror


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


         int main(int argc, char *argv[])
         {
            FILE *fout;

            if ((fout = fopen(argv[1], "w")) == NULL) {
               perror("fopen: Could not open file for writing");
               return EXIT_FAILURE;
            }
         return EXIT_SUCCESS;
         }


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


        Read Error handling online: https://riptutorial.com/c/topic/2486/error-handling







































        https://riptutorial.com/                                                                             133
   152   153   154   155   156   157   158   159   160   161   162