Wikipedia:wikiProject C/stdio.h/fclose

Source: Wikipedia, the free encyclopedia.


fclose is a C function belonging to the ANSI C standard library, and included in the file stdio.h. Its purpose is close a stream and all the structure associated with it. Notes that most of the time the stream is an open file. It has the following prototype:

int fclose(FILE *file_pointer)

It takes one argument: a pointer to the FILE structure of the stream to close, e.g.: :fclose(my_file_pointer) This line calls the function fclose to close the FILE stream structure pointed to by my_file_pointer.

The return value is an integer with the following signification:

  • 0 (zero) : the stream was closed successfully;
  • EOF : an error happened;

Note that one can check for the error number by using ''errno.h''.

Example usage

#include <stdio.h>

int main(int argc, char **argv) {
  FILE *file_pointer;
  int i;
  file_pointer = fopen("myfile.txt","r");
  fscanf(file_pointer, "%d", &i);
  printf("The integer is %d\n", i);
  fclose(file_pointer);
  return 0;
}

The above program opens a file called myfile.txt and scans for an integer in it.