gtpc2m25C/C++ Language Support User's Guide

fclose-Close File Stream

This function closes a file stream.

Format

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

stream
A pointer to the open file stream to be closed.

This function flushes a stream and then closes the file associated with that stream. The function then releases any buffers associated with the stream. To flush means that unwritten buffered data is written to the file and buffered data that is not read is discarded.

Note:
The storage pointed to by the FILE pointer is freed by the fclose function. An attempt to use the FILE pointer to a closed file is incorrect. This restriction is true even when the fclose function fails.

A pointer to a closed file cannot be used as an input value to the freopen function.

Normal Return

If successful, the fclose function returns 0.

Error Return

If a failure occurs when flushing buffers or in outputting data, EOF is returned. An attempt is still made to close the file.

The fclose function sets errno to one of the following:

EAGAIN
The O_NONBLOCK flag is set and output cannot be written immediately.

EBADF
The underlying file descriptor is not valid.

EFBIG
Writing to the output file would exceed the maximum file size or the file size of the process supported by the implementation.

ENOSPC
There is no free space left on the output device

Programming Considerations

None.

Examples

The following example opens the file myfile.dat for reading as a stream and then closes the file.

#include <stdio.h>
 
int main(void)
{
   FILE *stream;
 
   stream = fopen("myfile.dat", "r");

  ·
  ·
  ·
if (fclose(stream)) /* Close the stream. */ printf("fclose error\n"); }

Related Information

See Appendix E, Programming Support for the TPF File System for more information about TPF File System C Functions.