Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

fclose - Close a file and flush buffer

&pagelevel(4)&pagelevel

Definition

#include <stdio.h>

int fclose(FILE *fp);

fclose closes the file to whose FILE structure the file pointer fp points and releases fp.
Memory space that was dynamically allocated for this FILE structure (with fopen or
fopen64) is also freed. fclose calls the fflush function before the file is closed.

Return val.

0

EOF

The file has been closed.

fclose was not successful, because

  • fp is not assigned to a file (file already closed) or

  • an error occurred when flushing the buffer.

Notes

If the file pointer fp does not point to a FILE structure, the program aborts!

Whenever a program is terminated normally or by means of exit, an fclose is automatically
executed for each open file. Therefore, you need not call fclose explicitly unless you
want to close a file prior to program termination, e.g. to ensure that the limit for open files
(=2048) is not exceeded.

Record I/O

Since data is not buffered in the case of record I/O, there is no internal call to the fflush
function.

Example

The following program fragment closes the file pointed to by file pointer fp when the end of
the file is reached.

FILE *fp;
if(feof(fp))
  fclose(fp);

See also

fflush, close, fdopen, fopen, fopen64, exit