I'm trying to read the following file via inbound redirection, however the program is entering an infinite loop and with totally wrong readings. When I manually enter the data the program executes as expected, stopping when I press ctrl
+ d
.
Here's the file:
728.78 Ferrari 05 1050 8
722.00 Williams 19 950 2
728.87 McLaren 14 750 9
722.32 Renault 27 930 14
698.92 RedBull 03 920 5
727.56 ToroRoso 26 1000 12
718.55 Haas 08 960 7
728.01 Mercedes 77 950 1
728.65 Ferrari 07 1050 3
722.11 Renault 30 930 10
728.50 Sauber 94 1000 11
728.39 Mercedes 44 950 6
728.22 McLaren 02 750 13
722.76 Williams 18 950 4
700.01 RedBull 33 920 15
Here's the program:
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
float weight; // Peso do carro
char name[30]; // Nome do carro
int number; // Número do carro
int power; // Potência do carro
int position; // Posição do carro
} Car;
int main(int argc, char const *argv[])
{
Car car;
while (fscanf(stdin, "%f %s %d %d %d", &car.weight, car.name, &car.number, &car.power, &car.position) != EOF)
{
printf("%.2f %s %.2d %d %d\n", car.weight, car.name, car.number, car.power, car.position);
}
return 0;
}
What am I doing wrong?