fclose – Close a Stream
Posted on In Programmingfclose is a frequently used C standard library which closes the file associated with the stream and disassociates it.
Table of Contents
NAME
fclose – close a stream
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.
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.
ERRORS
EBADF The file descriptor underlying fp is not valid.
The fclose() function may also fail and set errno for any of the errors specified for the routines close(),
write() or fflush().
CONFORMING TO
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().
From Linux Programmer’s Manual
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; }