#include <stdio.h> int fclose(FILE *f);
fclose
disassociates the FILE
object addressed by f
from
the associated external file after writing out any buffered output data.
(By definition, standard I/O is always buffered.)
fclose
returns 0 if the file is closed successfully. It returns a
nonzero value if it is not closed successfully.
If fclose
fails, you cannot use the FILE
object addressed by
f
. The file is closed to you, and you have to reopen it. Because
fclose
flushes the output buffer, an error can occur in buffer flushing
with the result that fclose
has effectively failed. Even in this case,
further use of the FILE
pointer is not possible.
FILE
pointer (except as an argument to
freopen
) after the file is closed is erroneous.
FILE
s are automatically closed at normal program termination.
fclose
is useful in programs that deal with multiple files. Files
that are unused can be closed to save memory space and to keep within any
constraints on the number of files that may be open simultaneously.
#include <stdio.h> #define LENGTH 80 char data[LENGTH + 2]; FILE *ff, *nf; main() { /* Open FILE1 to read. */ ff = fopen("tso:FILE1", "r"); /* Open NEXTFILE to write. */ nf = fopen("tso:NEXTFILE", "w"); /* Read a maximum of 81 characters into the string, data. */ while (fgets(data, LENGTH + 2, ff)) fputs(data, nf); /* Write data into NEXTFILE. */ fclose(ff); fclose(nf); }
afflush
, close
, fflush
Copyright (c) 1998 SAS Institute Inc. Cary, NC, USA. All rights reserved.