Added error check on fflush()