Name

fclose - closes a stream

Library

libc.lib

Synopsis

  #include <stdio.h>
  int fclose (FILE *stream);

Return values

Upon successful completion 0 is returned. Otherwise, EOF is returned and the global variable errno is set to indicate the error. In either case no further access to the stream is possible.

Detailed description

The fclose function dissociates the named stream from its underlying file or set of functions. If the stream was being used for output, any buffered data is written first, using fflush.

Examples

/****************** this program shows opening and closing of a file using fclose api **************/
#include <stdio.h>
int main()
{
        FILE *fp;
        fp = fopen("c:\\input.txt", "w+");
        if(fp == NULL)
                {               
                printf("file opening failed");
                return -1;
                }
        
        printf("file opened successfully: Perform file operations now\n");
        
        if(!fclose(fp))
                {
                printf("file closed successfully");
                return 0;
                }
        else
                {
                printf("file closing failed");
                return -1;
                }
}

         

Output

file opened successfully: Perform file operations now
file closed successfully

         

Errors

The fclose function may also fail and set errno for any of the errors specified for the routines close or fflush.

Notes

The fclose function does not handle NULL arguments; they will result in a segmentation violation. This is intentional - it makes it easier to make sure programs written under
are bug free. This behaviour is an implementation detail, and programs should not rely upon it.


Feedback

For additional information or queries on this page send feedback

© 2005-2007 Nokia

Top