fclose, _fcloseall

Description

Closes a stream (fclose) or closes all open streams (_fcloseall).

#include <stdio.h>

int fclose( FILE *stream );

int _fcloseall( void );

stream Pointer to FILE structure  

Remarks

The fclose function closes stream. The _fcloseall function closes all open streams except stdin, stdout, stderr (and in DOS, stdaux and stdprn). It also closes and deletes any temporary files created by tmpfile.

In both functions, all buffers associated with the stream are flushed prior to closing. System-allocated buffers are released when the stream is closed. Buffers assigned by the user with setbuf and setvbuf are not automatically released.

Return Value

The fclose function returns 0 if the stream is successfully closed. The _fcloseall function returns the total number of streams closed. Both functions return EOF to indicate an error.

Compatibility

fclose

Standards:ANSI, UNIX

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:DOS32X

_fcloseall

Standards:None

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:DOS32X

See Also

_close, _fdopen, fflush, fopen, freopen

Example

/* FOPEN.C: This program opens files named "data" and "data2". It uses

* fclose to close "data" and _fcloseall to close all remaining files.

*/

#include <stdio.h>

FILE *stream, *stream2;

void main( void )

{

int numclosed;

/* Open for read (will fail if 'data does not exist) */

if( (stream = fopen( "data", "r" )) == NULL )

printf( "The file 'data' was not opened\n" );

else

printf( "The file 'data' was opened\n" );

/* Open for write */

if( (stream2 = fopen( "data2", "w+" )) == NULL )

printf( "The file 'data2' was not opened\n" );

else

printf( "The file 'data2' was opened\n" );

/* Close stream */

if( fclose( stream ) )

printf( "The file 'data' was not closed\n" );

/* All other files are closed: */

numclosed = _fcloseall( );

printf( "Number of files closed by _fcloseall: %u\n", numclosed );

}

Output

The file 'data' was opened

The file 'data2' was opened

Number of files closed by _fcloseall: 1