Fclose in c – fclose C Library Function

fclose in c: 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

Fclose in c: If a call to fclose is successful it returns zero, otherwise EOF is returned.

C program using fclose function

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

fclose C Library Function

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

Output

fclose C standard library function