-
What is best approach for error handling in c programming?
over 9 years ago
-
over 9 years ago
In C programming we don't have the error handling mechanism but at the point of view system programming language, it provides access at lower level in the form of return values. it can return -1 and NULL in any error and sets an error code errno is set which is global variable and indicates an error occurred during any function call. the various error codes defined in
header file. C programming language provides perror() and strerror() functions which can be used to display the text message associated with errno.
The Below code can represent the error using perror() and strerror() function if the file pointer value indicates to NULL that means the file not present in that location or not exist.
include <stdio.h> #include <errno.h> #include <string.h> extern int errno ; int main () { FILE * pf; int errnum; pf = fopen ("unexist.txt", "rb"); if (pf == NULL) { errnum = errno; fprintf(stderr, "Value of errno: %d\n", errno); perror("Error printed by perror"); fprintf(stderr, "Error opening file: %s\n", strerror( errnum )); } else { fclose (pf); } return 0; }
-
over 9 years ago
Hello Manish
Thanks again. -
2 Answer(s)