Latest web development tutorials

C library functions - perror ()

C standard library - <stdio.h> C standard library - <stdio.h>

description

C library functionsvoid perror (const char * str) to a descriptive error messages output to standard error stderr.First, the output stringstr, followed by a colon, followed by a space.

statement

Here is () statement perror function.

void perror(const char *str)

parameter

  • str - This is a C string containing a custom message will be displayed before the original error message.

return value

This function does not return a value.

Examples

The following example demonstrates the perror () function is used.

#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's compile and run the above program, which will produce the following results, as we try to open a file that does not exist:

Error: : No such file or directory

C standard library - <stdio.h> C standard library - <stdio.h>