Whats Coming

Chapter 14 - Advanced Programming Topics

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

Handling Errors—perror( )
One of the many interesting functions prototyped in STDIO.H is a function called perror( ). The function prints to the stderr stream the system error message for the last library routine called that generated an error. It does this by using errno and _sys_errlist, prototyped in STDLIB.H. The _sys_errlist value is an array of error message strings. The errno value is an index into the message string array and is automatically set to the index for the error generated. The number of entries in the array is determined by another constant, -_sys_nerr, also defined in STDLIB.H.
The function perror( ) has only one parameter, a character string. Normally, the argument passed is a string representing the file or function that generated the error condition. The following example demonstrates the simplicity of the function:
/*  
*  perror.c
*  A C program demonstrating the function perror( )
*  prototyped in stdio.h
*  Copyright (c) Chris H. Pappas and William H. Murray, 1998
*/

#include <stdio.h>

void main(void)
{
  FILE *fpinfile;
  fpinfile = fopen(“input.dat”, “r”);

  if (!fpinfile)
    perror(“Could not open input.dat in file main( ) :”);
}
The output from the program looks like the following:
Could not open input.dat in file main( ) : No such file or directory

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net