I’m not able to store data in the file it creates the file but only stores nothing

Asked

Viewed 44 times

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);
    }
}

1 answer

1

The aux pointer was created but not initialized, containing any value at the start of the loop. It is likely that your program is closing when trying to access the pointer, interrupting for segmentation failure. You must load the data to be recorded on aux pointer before starting the recording.

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

pizzas *aux = getPizzas();

Probably this program already has this data. So the correct thing would be to receive the data as a function parameter:

void Criar_Ficheiro_Pizza(pizzas *aux)

Search in the program for the text "pizzas" so you can find points where this data is generated.

  • how to do that is that I’m still learning programming...

  • I updated the answer, adding an explanation about where to get the data. If possible add the full code to facilitate the explanation.

Browser other questions tagged

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