fclose
fclose is a frequently used C standard library which closes the file associated with the stream and disassociates it.
fclose是一个常用的C标准库,它关闭与流关联的文件并取消关联。
名称 (NAME)
fclose – close a stream
fclose –关闭流
概要 (SYNOPSIS)
#include <stdio.h>
int fclose(FILE *fp);
描述 (DESCRIPTION)
The fclose() function will flushes the stream pointed to by fp (writing any buffered output data using fflush()) and closes the underlying file descriptor.
fclose()函数将刷新fp指向的流(使用fflush()写入任何缓冲的输出数据)并关闭基础文件描述符。
返回值 (RETURN VALUE)
Upon successful completion 0 is returned. Otherwise, EOF is returned and errno is set to indicate the error. In either case any further access (including another call to fclose()) to the stream results in undefined behaviour.
成功完成后,将返回0。 否则,将返回EOF并将errno设置为指示错误 。 在任何一种情况下,对该流的任何进一步访问(包括对fclose()的另一个调用)都会导致未定义的行为。
错误 (ERRORS)
EBADF The file descriptor underlying fp is not valid.
EBADF fp底层的文件描述符无效。
The fclose() function may also fail and set errno for any of the errors specified for the routines close(),
write() or fflush().
fclose()函数也可能会失败,并为例程close()指定的任何错误设置errno,
write()或fflush()。
符合 (CONFORMING TO)
C89, C99.
C89,C99。
笔记 (NOTES)
Note that fclose() only flushes the user space buffers provided by the C library. To ensure that the data is physically stored on disk the kernel buffers must be flushed too, for example, with sync() or fsync().
请注意,fclose()仅刷新C库提供的用户空间缓冲区。 为了确保将数据物理存储在磁盘上,内核缓冲区也必须进行刷新,例如,使用sync()或fsync()。
From Linux Programmer’s Manual
摘自Linux程序员手册
例 (EXAMPLE)
#include <stdio.h>
int main(int argc, char* args[])
{
FILE * fp;
pFile = fopen ("file_to_write.txt","wt");
fprintf (fp, "fclose example");
fclose (fp);
return 0;
}
fclose