file pointer

shyma_nk used Ask the Experts™
in C when we close a file pointer using fclose how can we be sure whether the file is properly closed or not?
FILE *ptr;
ptr = fopen("File1","w");
if ( ptr == NULL)
     printf(" closed the file ");
     printf("\n file not closed ");

can i check like this for the file closing?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Try to open file again . It shows some error.
FILE *fp;
if((fp=fopen("filename", "w"))==NULL)
 printf("error in opening file ");
Most Valuable Expert 2014
Top Expert 2015
On successful completion these fclose return a value of zero.
Otherwise EOF is returned.  EOF is returned if stream is NULL,
or stream is not active, or there was an error when flushing buffered
writes, or there was an error closing the underlying file descriptor.
From the fclose man page:

Upon successful completion 0 is returned.  Otherwise,  EOF
is  returned and the global variable errno is set to
indi­cate  the  error.   In  either  case  any  further  access (including another call to fclose()) to the stream results in undefined behaviour.

If u see an over lapping answers or calculations u can check that whether u have closed the file properly or not.


thank you or your help

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial