Error loading file to chained list

Asked

Viewed 198 times

0

This code carries elements from a file. txt, but apparently it can load the file information, but it gives some error in inserting the list, but I’m not able to identify. Follow the code and file information:

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

struct Cronograma {
    int data_cheg;
    char hora_cheg[50];
    char nome_navio[50];
    int carga_navio;
    struct Cronograma *prox;
};

typedef struct Cronograma cronograma;

void insere (cronograma *lista, int data, const char hora[], const char nome[], int carga) {
    cronograma *p, *novo;
    p = lista->prox;

    novo = malloc(sizeof(cronograma));
    novo->data_cheg = data;
    strcpy (novo->hora_cheg, hora);
    strcpy (novo->nome_navio, nome);
    novo->carga_navio = carga; 

    while (p->prox != NULL) 
        p = p->prox; 
    novo->prox = p->prox;
    p->prox = novo;
}

void carrega_arquivo (cronograma *lista) {
    cronograma *p = lista->prox;  
    FILE *f;
    int dc, c; char nv[50], hc[50];

    f = fopen ("crono.txt", "rt");

    if (f == NULL) {
        printf ("Problema na abertura do arquivo");
        return;
    }

    while (!feof(f)) {
        fscanf(f, "%d %s %[A-Z a-z] %d", &dc, hc, nv, &c); 
        printf("%d %s %s %d", dc, hc, nv, c);
        printf("\n");
        insere (p, dc, hc, nv, c);
    }
    fclose(f);
}

void imprime (cronograma *lista) {
    cronograma *p; 

    for (p = lista->prox; p != NULL; p = p->prox) {
        printf("%d %s %s %d", p->data_cheg, p->hora_cheg, p->nome_navio, p->carga_navio);
        printf("\n");
    }
} 

int main() {
    cronograma *Lista;
    Lista = malloc(sizeof(cronograma));
    Lista->prox = NULL;

    carrega_arquivo(Lista);

    return 0;
}


02 02:00 PANAMAX 80000
02 11:20 CAPE SIZE 180000 
03 04:00 HAND SIZE 50000 
03 12:00 SMALL CAPE SIZE 125000 
03 14:00 PANAMAX 80000 
04 09:30 HAND SIZE 50000 
04 09:30 CAPE SIZE 180000 
05 17:00 CAPE SIZE 180000 
05 19:00 CAPE SIZE 180000 
05 23:00 PANAMAX 80000 
06 06:00 PANAMAX 80000 
06 08:00 SMALL CAPE SIZE 125000 
06 15:00 HAND SIZE 50000 
06 16:00 HAND SIZE 50000 
08 02:00 CAPE SIZE 180000 
08 09:00 PANAMAX 80000 
09 10:00 SMALL CAPE SIZE 125000 
10 13:00 CAPE SIZE 180000 
10 14:00 19-HAND SIZE 50000 
10 19:00 CAPE SIZE 180000 
11 05:00 SMALL CAPE SIZE 125000 
11 10:00 PANAMAX 80000 
11 14:00 SMALL CAPE SIZE 125000 
12 13:00 CAPE SIZE 180000 
13 06:00 25-HAND SIZE 50000 
13 09:00 SMALL CAPE SIZE 125000 
14 15:00 PANAMAX 80000 
14 15:00 CAPE SIZE 180000 

1 answer

1


Marcelo, your problem is very simple. in the first line of the file load function, you assign the value list->Prox in the pointer to schedule, p.

cronograma *p = lista->prox;

causing p to receive NULL. In the case you are passing the null value assigned in the main function to p.

cronograma *Lista;
Lista = malloc(sizeof(cronograma));
Lista->prox = NULL;

so when you make the call of the function inserts you do again, in the second line of the function inserts

p = lista->prox;// (p = NULL->prox)

If in the file load function you change the first line to

cronograma *p = lista;

the program runs normally.

I hope I’ve helped.

Browser other questions tagged

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