Error loading file for threaded list

0

This code loads elements of a .txt file, but apparently it can load the information from the file, but it gives some error in inserting the list, but I can not identify it. Here is 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 
    
asked by anonymous 18.03.2017 / 06:11

1 answer

1

Marcelo, Your problem is very simple. in the first line of the function load_file, you assign the value-list-prox in the pointer to schedule, p.

cronograma *p = lista->prox;

causing p to get NULL. In 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 function call inserts you do it again, the second line of the function inserts

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

If in the function file_file you change the first line to

cronograma *p = lista;

The program runs normally.

I hope I have helped.

    
18.03.2017 / 11:53