perror() - I/O Function

How is "perror()" used in C++?
How to print error message?


perror() is an I/O function that prints the error message, maps the value of the global variable errno into a string and writes that string to the stderr. This function should be called right after the error has occured otherwise other function calls can overwrite this function.


void perror( const char *str);

Example :

#include <stdio.h>
int main()
FILE * fp= fopen("perroreg.txt", "r");
if ( fp == NULL)
perror("Example for PERROR function!!!!");
return 0;

Result :

Example for PERROR function!!!!: No such file or directory

In the above example the "perror()" is triggered as the specified file is not existent and the error message is printed.

C++ Tutorial

Ask Questions

Ask Question