HP C/iX Library Reference Manual (30026-90004)
Chapter 5 141
HP C/iX Library Function Descriptions
fclose
fclose
Closes an open file.
Syntax
#include <stdio.h>
int fclose (FILE *
stream
);
Parameters
stream
A pointer to the file to be closed.
Return Values
0 The file is successfully closed.
≠0 An error occurred. The file is not closed.
Description
The fclose function flushes the buffer associated with the specified stream, and, if the
buffer was allocated automatically by the standard I/O system, frees the space allocated to
that buffer. The stream is then closed, breaking the connection between your file pointer
and the stream. The fclose function closes files opened by the fopen(), fdopen(), or
freopen() functions.
The fclose function takes a pointer to FILE as its argument (returned from a call to
fopen(), fdopen(), or freopen()). The function posts any information written to the file
that is still in the stream's buffer, and it then closes the file. This disassociates the file and
the stream. If the buffer was automatically allocated, it is deallocated.
There are two reasons why you can open a file, but might never explicitly close the file.
First, notice that all programs in this chapter that open files end with a call to exit(). The
exit() call automatically performs an fclose() operation for every open file in that
program. Second, when a C program is compiled, an exit() call is normally compiled with
your code, so that if you return from main() or reach the } that terminates main(), it is
equivalent to calling exit().
See Also
exit(), fdopen(), fopen(), freopen(), setbuf(), ANSI C 4.9.5.1, POSIX.1 8.1