Failed to read formatted file via incoming redirect

Asked

Viewed 37 times

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?

  • 1

    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!

  • That’s weird. I compiled with gcc (Ubuntu 5.4.0-6ubuntu1~16.04.5) 5.4.0 20160609

  • I would say you have another problem. How are you doing redirecting?

1 answer

1


The problem with your code is that fscanf() cannot identify special end-of-line character \n, causing your while do not insert correctly on the input lines.

You can read line by line from your input through the function fgets() and then 'disassemble' each of these lines using the function sscanf(), look at you:

#include <stdio.h>
#include <stdlib.h>

#define LINHA_MAX_TAM   (100)

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(void)
{
    Car car;
    char linha[LINHA_MAX_TAM];

    while(fgets(linha, LINHA_MAX_TAM, stdin )) 
    {
        sscanf( linha, "%f %s %d %d %d", &car.weight, car.name, &car.number, &car.power, &car.position);
        printf("Peso: %.2f - Nome: %s - Numero: %.2d - Potencia: %d - Posicao: %d\n", car.weight, car.name, car.number, car.power, car.position);
    }

    return 0;
}

Testing:

$ cat teste.txt | ./cars
Peso: 728.78 - Nome: Ferrari - Numero: 05 - Potencia: 1050 - Posicao: 8
Peso: 722.00 - Nome: Williams - Numero: 19 - Potencia: 950 - Posicao: 2
Peso: 728.87 - Nome: McLaren - Numero: 14 - Potencia: 750 - Posicao: 9
Peso: 722.32 - Nome: Renault - Numero: 27 - Potencia: 930 - Posicao: 14
Peso: 698.92 - Nome: RedBull - Numero: 03 - Potencia: 920 - Posicao: 5
Peso: 727.56 - Nome: ToroRoso - Numero: 26 - Potencia: 1000 - Posicao: 12
Peso: 718.55 - Nome: Haas - Numero: 08 - Potencia: 960 - Posicao: 7
Peso: 728.01 - Nome: Mercedes - Numero: 77 - Potencia: 950 - Posicao: 1
Peso: 728.65 - Nome: Ferrari - Numero: 07 - Potencia: 1050 - Posicao: 3
Peso: 722.11 - Nome: Renault - Numero: 30 - Potencia: 930 - Posicao: 10
Peso: 728.50 - Nome: Sauber - Numero: 94 - Potencia: 1000 - Posicao: 11
Peso: 728.39 - Nome: Mercedes - Numero: 44 - Potencia: 950 - Posicao: 6
Peso: 728.22 - Nome: McLaren - Numero: 02 - Potencia: 750 - Posicao: 13
Peso: 722.76 - Nome: Williams - Numero: 18 - Potencia: 950 - Posicao: 4
Peso: 700.01 - Nome: RedBull - Numero: 33 - Potencia: 920 - Posicao: 15

See working on Ideone

  • I tried to execute this code but the exit of the first car is wrong: https://pastebin.com/Tncb8yBW

  • @Michaelpacheco If not solved, do not mark as accepted answer no.

  • Only the first record? Is the input file in plain text (ASCII) ? This behavior may be occurring because of something called GOOD, present in many UNICODE files.

  • @Michaelpacheco: See the code working on Ideone

Browser other questions tagged

You are not signed in. Login or sign up in order to post.