The GNU C Library - Closing Streams

Node: Closing Streams Next: Simple Output Prev: Opening Streams Up: I/O on Streams

Closing Streams

When a stream is closed with fclose , the connection between the stream and the file is cancelled. After you have closed a stream, you cannot perform any additional operations on it.

Function int fclose (FILE *stream)
This function causes stream to be closed and the connection to the corresponding file to be broken. Any buffered output is written and any buffered input is discarded. The fclose function returns a value of 0 if the file was closed successfully, and EOF if an error was detected.

It is important to check for errors when you call fclose to close an output stream, because real, everyday errors can be detected at this time. For example, when fclose writes the remaining buffered output, it might get an error because the disk is full. Even if you know the buffer is empty, errors can still occur when closing a file if you are using NFS.

The function fclose is declared in `stdio.h'.

If the main function to your program returns, or if you call the exit function (see Normal Termination), all open streams are automatically closed properly. If your program terminates in any other manner, such as by calling the abort function (see Aborting a Program) or from a fatal signal (see Signal Handling), open streams might not be closed properly. Buffered output might not be flushed and files may be incomplete. For more information on buffering of streams, see Stream Buffering.


Next: Simple Output Up: I/O on Streams