C Library Function - perror()
Description
C library function *void perror(const char str) Outputs a descriptive error message to the standard error stderr. First, it outputs the string str, followed by a colon, and then a space.
Declaration
The following is the declaration of the perror() function.
void perror(const char *str)
Parameters
- str -- This is a C string containing a custom message that will be displayed before the original error message.
Return Value
This function does not return any value.
Example
The following example demonstrates the use of the perror() function.
#include
int main ()
{
FILE *fp;
/* Rename the file */
rename("file.txt", "newfile.txt");
/* Now let's try to open the same file */
fp = fopen("file.txt", "r");
if( fp == NULL ) {
perror("Error: ");
return(-1);
}
fclose(fp);
return(0);
}
Let's compile and run the above program, which will produce the following result because we are trying to open a file that does not exist:
Error: : No such file or directory
C Standard Library -