fclose function-close file

The fclose function closes the file. You can use it by reading the stdio.h header.

int fclose (FILE * fp);

The argument is the file stream of the file you want to close. Returns 0 if the file close is successful, EOF if it is an error. The buffer is flushed when the file is closed. A buffer is a memory area that temporarily stores write data. Flash means writing out the data in the buffer.

sample fclose function

This is a sample of the fclose function. The fopen function opens the file, and the fclose function closes the file.

#include <stdio.h>
#include <stdlib.h>

int main (void) {
  
  // Open the file
  const char * out_file = "output.txt";
  FILE * out_fp = fopen (out_file, "w");
  if (out_fp == NULL) {
    fprintf (stderr, "Can't open file%s\n", out_file);
    exit (1);
  }
  
  // write to file
  const char * message = "Hello";
  fprintf (out_fp, "%s\n", message);
  
  // Close the file with the fclose function
  fclose (out_fp);
}

Associated Information