use fgets and sscanf instead of fscanf to avoid infinite loop