io out of loop and io bug fixed(fread,fwrite)