Edit

Share via


fclose, _fcloseall

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

Syntax

int fclose(
   FILE *stream
);
int _fcloseall( void );

Parameters

stream
Pointer to FILE structure.

Return value

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

Remarks

The fclose function closes stream. If stream is NULL, the invalid parameter handler is invoked, as described in Parameter validation. If execution is allowed to continue, fclose sets errno to EINVAL and returns EOF. It's recommended that you always check the stream pointer before you call this function.

For more information about return codes, see errno, _doserrno, _sys_errlist, and _sys_nerr.

The _fcloseall function closes all open streams except stdin, stdout, stderr (and, in MS-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 aren't automatically released.

Note

When fclose or _fcloseall functions are used to close a stream, the underlying file descriptor and OS file handle (or socket) are closed as well. Thus, if the file was originally opened as a file handle or file descriptor and is closed with fclose, don't also call _close to close the file descriptor; and don't call the Win32 function CloseHandle to close the file handle.

fclose and _fcloseall include code to protect against interference from other threads. For non-locking version of a fclose, see _fclose_nolock.

By default, this function's global state is scoped to the application. To change this behavior, see Global state in the CRT.

Requirements

Function Required header
fclose <stdio.h>
_fcloseall <stdio.h>

For more compatibility information, see Compatibility.

Example

See the example for fopen.

See also

Stream I/O
_close
_fdopen, _wfdopen
fflush
fopen, _wfopen
freopen, _wfreopen