Name
fclose, fcloseall - Close streams.
Syntax
#include <stdio.h>
int fclose(stream)
FILE *stream;
int fcloseall(void)
Description
The fclose function closes the given stream. The fcloseall
function closes all open streams except stdin, stdout,
stderr (and in DOS, stdaux and stdprn). It also closes and
deletes any temporary files created by tmpfile. All buffers
associated with the stream are flushed prior to closing.
System-allocated buffers are released when the stream is
closed. Buffers assigned by the user with setbuf and setvbuf
are not automatically released.
Return Value
The fclose function returns 0 if the stream is successfully
closed. The fcloseall function returns the total number of
streams closed. Both functions return EOF to indicate an
error.
See Also
close(S), fdopen(S), fflush(S), fopen(DOS), freopen(DOS)
Example
#include <stdio.h>
FILE *stream, *stream2;
main()
{
int numclosed;
/* Two files are opened: */
stream = fopen("data", "r" );
stream2 = fopen("data2","w+");
if (stream == NULL)
printf("The file data was not opened\n");
else
{
fclose(stream);
printf("The file 'data' closed\n");
}
/* All other files are closed: */
numclosed = fcloseall();
printf("The function fcloseall");
printf("closed %u files\n", numclosed);
}
This program opens files named data and data2. It uses
fclose to close data and fcloseall to close all remaining
files.
(printed 6/18/89)