fclose -- Close a File

SYNOPSIS

 #include <stdio.h>

 int fclose(FILE *f);
 

DESCRIPTION

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.)

RETURN VALUE

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.

DIAGNOSTICS

Any attempt to use a FILE pointer (except as an argument to freopen) after the file is closed is erroneous.

IMPLEMENTATION

All open FILEs are automatically closed at normal program termination.

USAGE NOTES

Because most implementations limit the number of files that can be open at one time, 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.

EXAMPLE

  #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);
  }

 

RELATED FUNCTIONS

afflush, close, fflush

SEE ALSO


Copyright (c) 1998 SAS Institute Inc. Cary, NC, USA. All rights reserved.