I am not able to save data in the file it creates the file but only it does not store anything [closed]

1
void Criar_Ficheiro_Pizza()
{
    FILE *B;
    if ((B = fopen("Pizzas.txt", "a")) == NULL)
    printf("\n\t !!! Ficheiro nao pode ser guardado!!!");
    else
    {
        pizzas *aux;
        while(aux!=NULL)
        {
            fprintf(B,"%d\t %c\t %.2f\t\n", aux->codpizza, aux->descricao, aux->preco);
            aux=aux->prox;
        }
        fclose(B);
    }
}
    
asked by anonymous 31.03.2017 / 22:41

1 answer

1

The pointer to aux was created but not initialized, containing any value at the start of the loop. It is likely that your program is closing when it tries to access the pointer, interrupting it by segmentation failure. You must load the data to be written to the aux pointer before you start recording.

To load the data into aux, you need to call some function that returns this data, for example:

pizzas *aux = getPizzas();

This program probably already has this data. So the correct would be to receive the data as a parameter of the function:

void Criar_Ficheiro_Pizza(pizzas *aux)

Search the program for the text "pizzas" because you will be able to find points at which this data is generated.

    
31.03.2017 / 22:59