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:
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