C Library Functions -perror()
C Standard Library - <stdio.h>
Description
C Library Functionsvoid perror(const char *str)Output a descriptive error message to standard error stderr. First output the stringstr, followed by a colon, and then a space.
Declaration
The following is the declaration of the perror() function.
void perror(const char *str)
Parameter
- str-- This is a C string that contains a custom message, which will be displayed before the original error message.
Return Value
This function does not return any value.
Example
The following example demonstrates the usage of the perror() function.
#include <stdio.h>
int main ()
{
FILE *fp;
/* 首先重命名文件 */
rename("file.txt", "newfile.txt");
/* 现在让我们尝试打开相同的文件 */
fp = fopen("file.txt", "r");
if( fp == NULL ) {
perror("Error: ");
return(-1);
}
fclose(fp);
return(0);
}
Let us compile and run the above program, which will produce the following result, because we attempt to open a file that does not exist:
Error: : No such file or directoryother extensions