perror() – I/O Function

perror() – I/O Function

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

Explanation

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.

Syntax:

void perror( const char *str);

Example :

#include
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.





Leave a Reply

Your email address will not be published. Required fields are marked *