fclose Function

Close a stream.

Include

<stdio.h>

Prototype

int fclose(FILE *stream);

Argument

stream pointer to the stream to close

Return Value

Returns 0 if successful; otherwise, returns EOF if any errors were detected.

Remarks

fclose writes any buffered output to the file. This function requires a heap.

Example

#include <stdio.h> /* for fopen, fclose,     */
                   /* printf,FILE, NULL, EOF */

int main(void)
{
  FILE *myfile1, *myfile2;
  int y;

  if ((myfile1 = fopen("afile1", "w+")) == NULL)
    printf("Cannot open afile1\n");
  else
  {
    printf("afile1 was opened\n");

    y = fclose(myfile1);
    if (y == EOF)
      printf("afile1 was not closed\n");
    else
      printf("afile1 was closed\n");
  }
}

Example Output

afile1 was opened
afile1 was closed