PRB: _cexit() Does Not Close Files Correctly

ID Number: Q69539

6.00 6.00a 6.00ax | 6.00 6.00a

MS-DOS | OS/2

buglist6.00 buglist6.00a buglist6.00ax fixlist7.00

Summary:

SYMPTOMS

As stated in the online help, the _cexit() function performs clean-

up operations and returns without terminating the process. The

_cexit() function first calls the functions registered by the

atexit() and onexit() routines, and then it should flush all I/O

buffers and close all open files before returning.

The _cexit() function works as described except that it does not

close all the open files. The sample code below demonstrates this

problem.

STATUS

Microsoft has confirmed this to be a problem in C versions 6.0,

6.0a, and 6.0ax. This problem was corrected in C/C++ version 7.0.

More Information:

Sample Code

-----------

*\

#include<stdio.h>

#include<stdlib.h>

#include<process.h>

void func(void);

void main(void)

{

FILE *outfile;

int retval;

atexit(func); /* set up function to invoke on exit */

if ( (outfile = fopen("TEST.TXT", "w")) == NULL )

{

printf("Could not open test file TEST.TXT");

exit(-1);

}

retval = sprintf(outfile, "This is line 1.\n");

if ( retval >= 0 )

printf("\nFirst line written successfully to file.\n");

else

printf("\nFile write failed on first attempt!\n");

_cexit(); /* Call _cexit() -- files should all be closed */

retval = sprintf(outfile, "This line should not be written!\n");

if ( retval >= 0 )

printf("Error - Write to file succeeded for file that should"

" have been closed!\n");

else

printf("File write failed on second attempt as expected!\n");

fclose(outfile);

}

void func(void)

{

printf("\nThis is the exit function code.\n\n");

}

Additional reference words: 6.00 6.00a 6.00ax