-1

So, im trying to import data from a txt file and store it into a diferent parts of a structure but im not sure what's the best way to do so. the txt file has to have this information:

id(double) time1(int) time2(int) risk(float) state1(int) state2(int) prerequisites(int)

ex:

10 15 16 20 1 0 1357

i tried this piece of code to test if the import of the txt file worked but it gives me no output:

typedef struct 
{
    double id; 
    int tmph; /* tempo de duracao do humano*/
    int tmpr; /* tempo de duracao do robo*/
    float risco;
    int prec[MAXTAREFAS]; /*precedencias*/
    int hum; /* humano pode realizar (=1) ou nao (=0)*/
    int robo; /* robo pode realizar (=1) ou nao (=0)*/
} Tarefa;

int main()
{
    Tarefa *t;
    int cont=1;
    printf("a");
    char *doc = "tarefateste.txt";
    FILE *file = fopen(doc, "r");
    if(file==NULL)
    {
        printf("Erro ao abrir o ficheiro");
        return 1;
    }
    while(fscanf(file, "%f %f %f %f %f %f %f \n", &t->id, &t->tmph, &t->tmpr, &t->risco, &t->prec[0], &t->hum, &t->robo) == 7)
    {
        if(t->id<0)
        {
            printf("Erro a importar id\n");
            return 1;
        }
        if(t->tmph<0)
        {
            printf("Erro a importar tmph\n");
            return 1;
        }
        if(t->tmpr<0)
        {
            printf("Erro a importar tmpr\n");
            return 1;
        }
        if(t->risco<0)
        {
            printf("Erro a importar risco\n");
            return 1;
        }
        if(t->prec[0]<0)
        {
            printf("Erro a importar prec\n");
            return 1;
        }
        else
        {
            if(t->prec[0]>=10)
            {
                while(t->prec[0]!=0)
                {
                    t->prec[cont]=t->prec[0] % 10;
                    t->prec[0]=t->prec[0]/10;
                    cont++;
                }
            }
        }
        if(t->hum!=0 || t->hum!=1)
        {
            printf("Erro a importar hum\n");
            return 1;
        }
        if(t->robo!=0 || t->robo!=1)
        {
            printf("Erro a importar robo\n");
            return 1;
        }
    }
    printf("%f\n", t->id);
    printf("%f\n", t->tmph);
    printf("%f\n", t->tmpr);
    printf("%f\n", t->risco);
    printf("%f\n", t->prec[1]);
    printf("%f\n", t->prec[2]);
    printf("%f\n", t->prec[3]);
    printf("%f\n", t->prec[4]);
    printf("%f\n", t->hum);
    printf("%f\n", t->robo);
    return 0;
}

0 Answers0