We have to define the error string first with the errno.Linux and Unix tee command tutorial with examples | George Ornbo We can also define the string error by ourselves. The strerror function takes the errno as an argument and returns this in a string form.īecause we are attempting to open a file that isn’t present, let’s compile and run the program mentioned above, which will result in the following outcome:Īs the previous code generates the error string itself when this occurs. If the fp is found NULL, then the printf statement is executed which has the strerror function. The file is assigned a file name with the read mode inside the fopen function if the condition is applied to the fp pointer variable. Within the main function section, we have created the file pointer “fp” with the FILE object. Printf ( "Error: %s \n", strerror (errno ) ) Īfter including the header file, we have the main function declaration. The caller of a few functions must set errno to 0 and examine it afterward to check if an error was found. The majority of functions report errors by returning a specific value, generally NULL for methods that return pointers and -1 for methods that return integers. Regardless of whether they notice mistakes, any library function may change the value that is saved before returning. The only values that library functions store are ones greater than zero. Certain library functions that detect errors save a value such that the error number is in the errno variable. It defines macros with the errno symbol to report and retrieve error circumstances (abbreviation for “error number”). The C programming language’s standard library contains the header file errno.h. As this is the code of the strerror function to access this function, we need to import an error.h file for this function. However, the name is a little deceptive because it comprises macro declarations, constants, and declarations of methods and types, These are used for handling multiple memory handling functions in addition to managing strings. The header for the C standard library is called string.h. Then, the string.h is added after the stdio.h header file. We must include the header file stdio.h to add the input/output-related methods in our program. We are required to include some library header files of C, which are required for this code implementation.įirst, we have inserted the stdio.h header file. Let’s discuss the implementation for this strerror function. We will utilize this in our C program to generate the error upon the execution. Here, we have demonstrated the utilization of the strerror function. When the error description string could not be stored on the provided storage, the error generated is ERANGE. If the errnum’s value is an invalid error number, the EINVAL error is thrown. Furthermore, the strerror function generates two errors, EINVAL and ERANGE. The value of errno remains unchanged if the operation is successful if not, it is changed to a nonzero value. Otherwise, it gives the relevant error description string. If the runtime error is unknown, the strerror() function provides an unknown error message. The argument is errnum: a specific error for which you’d like to receive the error message. We have only one argument which the strerror function takes. The string that explains the mistake is obtained using this strerror syntax. We use the following syntax of the function strerror() in the C language. Syntax of the strerror Function in C Language Let’s investigate the C language’s strerror function in greater detail. You can get the default error message for a certain error code using the strerror methods, and you can quickly get the name of the program that made the error using the variable program invocation short name. The library contains variables and functions intended to make it simple for your program to provide detailed error message failure on the part of a library call in the usual format. A locale-specific error message string is not produced by this function. Depending on the compiler and platform used for development, strerror generates error strings, and converts a string of error messages to the error number in errnum.Įrrno values with proper errnums are required. The C library calls char *strerror(int errnum), which produces a reference to a string comprising of the error message, and searches an internal array for the error number errnum. If this occurs, the function returns -1, and your code must rely on our trusty errno to figure out what went wrong and perhaps emit an instructive error message. As soon as your program tries to reach the operating system, something goes wrong.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |