Purpose

The fclose function is used to close an open FILE stream. If the stream was being used for output, any buffered data is written first.

Header File

stdio.h
(fclose)

Prototype

int fclose (FILE * Stream);

Argument

Stream
Pointer to an open FILE structure

Return Values

0
The file is closed successfully.
!=0
The file is not closed.  An error occurred.

Example

#include <stdio.h>

int main () {
  /* Local Declarations                      */
  FILE * Input;
  char C;
  char FileName[20];

  /* Initialization                          */
  printf ("File name?  ");
  scanf ("%s", FileName);
  Input = fopen (FileName, "r");

  /* Algorithm                               */
  fread (&C, 1, 1, Input);
  while (!feof(Input)) {
    fwrite (&C, 1, 1, stdout);
    fread (&C, 1, 1, Input);
  }

  /* Finalization                            */
  fclose (Input);

  /* Report to system                        */
  return 0;
}

See Also

fopen   Open a FILE stream

External Links

fclose   HP 3000 Library Reference Manual
Closing Streams   The GNU C Library
fclose   The Open Group Base Specifications
feof   MSDN

Valid HTML 4.01 Transitional

Valid CSS

Site Icon Copyright © 1999-2007, jhyoung, revised 4/30/2007