Fix invalid fread call in parse_file and clean up error handling