Made a few small tweaks to the code namely replacing fopen, fwrite and fclose with a single command.