![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
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 Ccompliant 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
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?
|
![]() |
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.
|