fclose、_fcloseallfclose, _fcloseall

关闭流(fclose)或关闭所有打开的流( _fcloseall)。Closes a stream (fclose) or closes all open streams (_fcloseall).


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


指向文件结构的指针。Pointer to FILE structure.

返回值Return Value

如果流已成功关闭, fclose将返回0。fclose returns 0 if the stream is successfully closed. _fcloseall返回已关闭的流总数。_fcloseall returns the total number of streams closed. 这两个函数都返回EOF来指示错误。Both functions return EOF to indicate an error.


Fclose函数关闭The fclose function closes stream. 如果streamNULL,则会调用无效参数处理程序,如参数验证中所述。If stream is NULL, the invalid parameter handler is invoked, as described in Parameter Validation. 如果允许执行继续,则fclose会将Errno设置为EINVAL并返回EOFIf execution is allowed to continue, fclose sets errno to EINVAL and returns EOF. 建议在调用此函数之前始终检查指针。It is recommended that the stream pointer always be checked prior to calling this function.

有关这些代码以及其他错误代码的详细信息,请参阅 _doserrno、errno、_sys_errlist 和 _sys_nerrSee _doserrno, errno, _sys_errlist, and _sys_nerr for more information on these, and other, error codes.

_Fcloseall函数关闭所有打开的流,除了stdinstdoutstderr (在 MS-DOS、 _stdaux_stdprn中)。The _fcloseall function closes all open streams except stdin, stdout, stderr (and, in MS-DOS, _stdaux and _stdprn). 它还关闭并删除由tmpfile创建的所有临时文件。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. 用户使用setbufsetvbuf分配的缓冲区不会自动释放。Buffers assigned by the user with setbuf and setvbuf are not automatically released.

注意: 当使用这些函数关闭流时,基础文件描述符和操作系统文件句柄(或套接字)以及流都将关闭。Note: When these functions are used to close a stream, the underlying file descriptor and OS file handle (or socket) are closed, as well as the stream. 因此,如果最初以文件句柄或文件描述符的形式打开该文件,并使用fclose关闭该文件,则也不要调用 _close来关闭文件描述符;请勿调用 Win32 函数CloseHandle来关闭文件句柄。Thus, if the file was originally opened as a file handle or file descriptor and is closed with fclose, do not also call _close to close the file descriptor; do not call the Win32 function CloseHandle to close the file handle.

fclose_fcloseall包括用于防范来自其他线程的干扰的代码。fclose and _fcloseall include code to protect against interference from other threads. 有关fclose的非锁定版本,请参阅 _fclose_nolockFor non-locking version of a fclose, see _fclose_nolock.


函数Function 必需的标头Required header
fclosefclose <stdio.h><stdio.h>
_fcloseall_fcloseall <stdio.h><stdio.h>

有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.


请参阅 fopen 的示例。See the example for fopen.

请参阅See also

流 I/OStream I/O
_fdopen、_wfdopen_fdopen, _wfdopen
fopen、_wfopen_wfopenfopen, _wfopen
freopen、_wfreopenfreopen, _wfreopen