Table of Contents

fclose

#include <stdio.h> /* including standard library */
 
    int fclose( FILE *stream );

Description

Close the current stream / file
Used to close the specified file (stream). You should always use this function, to clean up after using a file.
Any data that has been written to the file, but not actually made it that far yet, i.e. which has only been written to the file buffer in memory,
will then be actually written to the physical file itself by the operating system.
The space in memory allocated for these buffers will also be deallocated. Any user defined buffers (specified by setbuf and setvbuf will however, not be deallocated.
Any temp files associated with the stream will also be deleted.

Returns EOF on error, zero otherwise.

C Sourcecode Example

/* 
 * fclose example code
 * http://code-reference.com/c/stdio.h/fclose 
 */
#include <stdio.h> /* including standard library */
//#include <windows.h> /* uncomment this for Windows */
 
 
int main( void )
{
  FILE *handle;
  handle = fopen("test.txt","r");
  if ( handle != 0 )
  {
    printf("fopen success:\n");
    fclose(handle);
  } else {
         printf("fopen failure\n");
 
         if (handle != NULL) {
             fclose(handle); 
          }
         return 1;
  }
  return 0;
}

fclose output

  output: if no test.txt file there
  user@host:~$ ./fclose 
  fopen failure
  output: if test.txt exist
  user@host:~$ ./fclose 
  fopen success

see also

fopen() freopen() fflush()