fclose Function

fclose <stdio.h>

int fclose( FILE * );
Description:

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.

Return Values:

Zero if successful, otherwise it returns EOF.

Example:
#include <stdio.h>

int main(void)
{
    char buffer[256];
    FILE * myfile;
	
    myfile = fopen("some.txt","r");
	
    while (!feof(myfile))
    {
        fgets(buffer,256,myfile);
        printf("%s",buffer);
    }
    
    return 0;
}

This example opens a file called some.txt, reads all the lines from the file and outputs them to the screen, then closes the file.

top
Home / Return to Function Reference