Click Here!
home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
[an error occurred while processing this directive]
Previous Table of Contents Next


3.  Define a global variable that will be used to hold some memory.
char* str;
4.  Inside main, enter code to set the function called when the program exits, allocate some memory, and call exit.
void main()
{
   int retVal = atexit(FinalCleanup);

   if (retVal != 0)
      printf(“Could not set exit function\n”);
   else
   {
      str = (char*)malloc(10);
      exit(1);
   }
}
5.  Implement the FinalCleanup function. This function will print a diagnostic message and free the memory previously allocated in main.
void FinalCleanup(void)
{
   printf(“\nCleaning up allocated memory.\n”);

   free(str);
}
6.  The following is the output for this sample program:
Cleaning up allocated memory.

How It Works

The exit function causes the program to be terminated normally. Unlike abort, exit does not print a diagnostic message. You can return an exit status code to the operating system or the caller of your program by specifying that exit status when you call the exit function.

You can register a function that will be called when exit is called or main returns by using the atexit function. The function you register with atexit must take no input parameters and return void. Anytime the program exits normally, your registered function will be called. You can register multiple functions with atexit and you can register the same function more than once. If you have called atexit multiple times, the registered functions are called in reverse order of their registration. In other words, the functions are executed in last-in first-out (LIFO) order.

The exit function will perform clean up before exiting. On ANSI C–compliant compiler implementations, all functions registered with atexit are called in reverse order of their registration. Then, all output streams are flushed and all open streams are closed. Next, files that were previously created using the tmpfile Standard C Library routine are removed. Finally, control is returned to the operating system or caller of this program with the return value specified in the call to exit.

The sample code listed in the “Steps” section registers the FinalCleanup function with atexit. When the program exits normally, FinalCleanup is called. It then prints a diagnostic message and frees memory that was previously allocated in main.

Comments

Using the exit function, you can return status or error codes to the operating system or to other programs. Using the atexit function, you can be sure all resources are freed when exiting your program normally. As you can see, if you use the techniques presented in this How-To and How-To 10.5 together, you can almost always be sure resources are freed whether your program exits normally or abnormally.

10.7 Detect errors that occur when reading from or writing to a file using the file functions provided with the Standard C Library?

Problem

When working with files, is there any way to detect errors that occur when reading from or writing to a file?

Technique

The functions for dealing with a file provided by the Standard C Library typically deal with the file as a stream. The ferror function can be used to test the stream for a read or write error.

The following section discusses how to test a file stream for a read or write error and how it works.

Steps

1.  Include stdio.h for standard I/O operations and errno.h in order to have access to the errno variable.
#include <stdio.h>
#include <errno.h>
2.  The code in the remaining steps should be placed in main.
3.  Declare variables to hold the file pointer of an open file, the data read from a file stream, the number of bytes read, and an err value that will be returned from ferror.
FILE *fptr;
char  buf[81];
int   errVal;
4.  Clear the errno variable and open a file for write-only access.
errno = 0;
fptr = fopen(“NewFile”, “w”);
5.  If the open failed, print an error message. Otherwise, attempt to read from the file that is open for write-only access. Test for an error condition using ferror and print a message if the read operation failed. Finally, clear the error condition and close the file.
if (fptr == NULL)
      perror(“Could not open NewFile”);
else
{
   errno = 0;
 fread(buf, sizeof(char), sizeof(buf), fptr);

   errVal = ferror(fptr);
   if (errVal)
   {
      printf(“Error %d reading stream\n”, errVal);
      clearerr(fptr);
   }

   fclose(fptr);
}
6.  The following is the output for this sample program:
Error 32 reading stream

How It Works

The ferror function tests for an error condition that might have occurred when reading from or writing to a stream. It will return the error status to the caller. If the error status is not equal to 0, an error has occurred. The ferror function will continue to return errors unless the clearerr function is called. Closing the stream with fclose will also clear the error condition.

The sample code in the “Steps” section opens a file for write-only access and attempts to read from it. This will cause an error to occur. The ferror function is called to test for an error. A diagnostic message is printed if there is an error. Next, clearerr is called to clear the error condition.

Comments

The ferror function is very useful when testing the status of a read or write operation to a file stream. However, if you are going to make repeated calls to ferror, remember to call clearerr each time.

10.8 Use setjmp and longjmp to maintain state when handling errors?

Problem

Is there another way to handle errors in my code instead of returning error values from functions or using raise and signal?


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.