Swap out fgets/printf for fread/fwrite

Add in fclose, use fopen not fdopen