C fclose()

The fclose() function defined in the stdio.h header file. It helps to close the stream pointed by the specified 'stream'. Before closing, it deletes all buffers that are associated with the stream.


int fclose(FILE *stream); #where stream should be a file pointer

 

fclose() Parameters: 

The fclose() function takes a single parameter.If a binary stream is closed, the file's last record is padded with null characters (\0) to the end of the record.

Parameter Description Required / Optional
stream the pointer to a FILE object that specifies the stream to be closed Required

fclose() Return Value

The return value of fclose() function is zero if the stream is successfully closed and EOF in case of failure.

Input Return Value
stream closed zero
stream not closed EOF

Examples of fclose()

Example 1: Working of fclose() function in C?


#include <stdio.h>

int main()
{
   FILE *pnt;
 
   pnt= fopen("file.txt", "w");

   fprintf(pnt, "%s", "C programming");
   fclose(pnt);
   
   return(0);
}

Output:


C programming

Example 2: How fclose() works in C?


#include <stdio.h>

int main (){
    FILE *pnt;
    pnt = fopen("fileName.txt","w");
    fprintf(pnt, "%s", "File test");
    fclose(pnt);
    return 0; 
}

Output:


File test