Syntax:
#include <cstdio> void perror( const char *str );
The perror() function prints str and an implementation-defined error message corresponding to the global variable errno. For example:
char* input_filename = "not_found.txt"; FILE* input = fopen( input_filename, "r" ); if( input == NULL ) { char error_msg[255]; sprintf( error_msg, "Error opening file '%s'", input_filename ); perror( error_msg ); exit( -1 ); }
If the file called not_found.txt is not found, this code will produce the following output:
Error opening file 'not_found.txt': No such file or directory