fclose : <stdio.h> library function

The function int fclose(FILE *stream); closes the file associated with stream and flushes its buffer. After a fclose call, stream is no longer connected with the file and all internal buffers associated with the stream are deallocated from it and flushed. It writes the contents of any unwritten output buffer before disassociating it with stream.

Function prototype of fclose

int fclose(FILE *stream);
  • stream : A pointer to a FILE object which identifies a stream.

Return value of fclose

If a call to fclose is successful it returns zero, otherwise EOF is returned.

C program to show the use of fclose function

The following program shows the use of fclose function to close a file(stream) after writing a sentence in it.

#include <stdio.h>

int main (){
  FILE * pFile;
  
  /* Creates a new file */
  pFile = fopen ("TextFile.txt","w");
  /* Write a sentence in a file */
  fprintf (pFile, "fclose C standard library function");
  /* Close a file */
  fclose (pFile);
  
  return 0;
}

Program Output
fclose C standard library function