fclose -- Close Stream

Format

#include <stdio.h>
int fclose(FILE *stream);

Language Level: ANSI, POSIX, XPG4
fclose closes a stream pointed to by stream. This function flushes all buffers associated with the stream before closing it. When it closes the stream, the function releases any buffers that the system reserved. When a binary stream is closed, the last record in the file is padded with null characters (\0) to the end of the record.

Return Value
fclose returns 0 if it successfully closes the stream, or EOF if any errors were detected.

Note: Once you close a stream with fclose, you must open it again before you can use it.

Example
This example opens a file fclose.dat for reading as a stream; then it closes this file.

#include <stdio.h>
#define FILENAME "myfile.dat"
#define NUM_ALPHA  26
int main(void)
{
  FILE *stream;
  char buffer[NUM_ALPHA];
  if (( stream = fopen("FILENAME", "r"))!= NULL )
  {
    fread( buffer, sizeof( char ), NUM_ALPHA, stream );
    printf( "buffer = %s\n", buffer );
  }
  if (fclose(stream))   /* Close the stream. */
      perror("fclose error");
  else printf("File FILENAME closed successfully.\n");
  return 0;
  /*******************************************************
     The output should be:
     File FILENAME closed successfully.
  *******************************************************/
}


close -- Close File Associated with Handle
_fcloseall -- Close All Open Streams
fflush -- Write Buffer to File
fopen -- Open Files
freopen -- Redirect Open Files
<stdio.h>