fclose() — Close Stream

Format

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

Language Level: ANSI

Threadsafe: Yes.

Description

The fclose() function closes a stream pointed to by stream. This function deletes all buffers that are associated with the stream before closing it. When it closes the stream, the function releases any buffers that the system reserved. When a binary stream is closed, the last record in the file is padded with null characters (\0) to the end of the record.

Return Value

The fclose() function returns 0 if it successfully closes the stream, or EOF if any errors were detected.

The value of errno can be set to:

Value
Meaning
ENOTOPEN
The file is not open.
EIOERROR
A non-recoverable I/O error occurred.
EIORECERR
A recoverable I/O error occurred.
ESCANFAILURE
The file was marked with a scan failure.
Note:
The storage pointed to by the FILE pointer is freed by the fclose() function. After the use of the fclose() function, any attempt to use the FILE pointer is not valid.

Example that uses fclose()

This example opens a file myfile for reading as a stream; then it closes this file.

#include <stdio.h>
 
#define NUM_ALPHA  26
 
int main(void)
{
  FILE *stream;
  char buffer[NUM_ALPHA];
 
  if (( stream = fopen("mylib/myfile", "r"))!= NULL )
  {
    fread( buffer, sizeof( char ), NUM_ALPHA, stream );
    printf( "buffer = %s\n", buffer );
  }
 
  if (fclose(stream))   /* Close the stream. */
      perror("fclose error");
  else printf("File mylib/myfile closed successfully.\n");
}

Related Information



[ Top of Page | Previous Page | Next Page | Contents | Index ]