added file close for destination files

It's a quick fix which closes the destination file after writing into it.
- store file descriptor in a variable
- after all the operations on file is done, close it using the file descriptor

More code refactor needs to be done which will be taken up next.