2
I am trying to read the following file via input redirect, but the program is going into 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 is 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 is 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?
Your code is perfectly functional. See in this video (https://asciinema.org/a/HBemldntsuM1haPmdjkTpexuY) that I recorded from my Linux terminal. It has been compiled with GCC. What may be happening is that you are doing something wrong by redirecting an output to the program entry. Or your compiler or IDE might have a bug. Please pass more information, please - just don’t say it’s Dev-CPP!
– José
That’s weird. I compiled with gcc (Ubuntu 5.4.0-6ubuntu1~16.04.5) 5.4.0 20160609
– Michael Pacheco
I would say you have another problem. How are you doing redirecting?
– José