save a data type in a file in c

Asked

Viewed 346 times

1

I have this data structure:

typedef struct
{
    ListaCartao tabelaCartao[HASHSIZE];
    ListaArtigos tabelaArtigos[HASHSIZE];
    char localidade[MAXLOCALIZACAO];
}tCelulaSuperdume, *SuperDume;

SuperDume superMercadoLocal[CONJUNTOSUPERDUME];
char localSuperDume[MAXLOCALIZACAO];
int indiceLocalSuperDume = 0;

Within SuperDume I have two Hash tables. Then I try to save one SuperDume file and I can’t:

void EscreveFicheiro(char nomeFich[])
{
    FILE* f;
    if (fopen_s(&f, nomeFich, "wb") == NULL)
        printf("Erro");

    else
    {
        SuperDume aux;
        for (i = 0; i < CONJUNTOSUPERDUME; i++)
        {
            aux = superMercadoLocal[indiceLocalSuperDume];          
            fwrite(&aux, sizeof(SuperDume) * CONJUNTOSUPERDUME,1,f);
        }
        fclose(f);

    }
}

The file opens, only it can’t do the fwrite, returning me the following error:
Erro

Edited. I have the following code

void EscreveFicheiroCartoes(char nomeFich[])
{
    FILE* f;

    if (fopen_s(&f, nomeFich, "wb") == NULL)
        printf("Erro");

    ListaCartao aux;
    for (i = 0; i < HASHSIZE; i++)
    {
        aux = superMercadoLocal[indiceLocalSuperDume]->tabelaCartao[i];

        if (aux == NULL)
            continue;

        else
        {
            while (aux != NULL)
            {
                fwrite(&(aux), sizeof(ListaCartao), 1, f);
                aux = aux->proximo;
            }
        }
    }
    fclose(f); 
}

2 answers

1

I can’t find the description of fopen_s in the C Standard; you must be using a Non-standard compiler :-)

To description of fopen_s provided by Microsoft and the way you use the value returned by the function are not coherent ... so I take it that’s not the version you’re using either.

Check your compiler’s manual for how yours works fopen_s.

Or use the Standard function that has proven itself over so many years

f = fopen(nomefich, "wb");
if (f == NULL) {
    perror(nomefich);
    exit(EXIT_FAILURE);
}

Edit: I found the fopen_s in Standard C11, in Appendix K, as optional.

The description in the Standard is consistent with Microsoft’s description.

Returns

The fopen_s Function Returns zero if it opened the file. If it Did not open the file or if there was a Runtime-Constraint Violation, fopen_s Returns a nonzero value.

That is, when the function returns 0 is because "it worked"

// NULL é automaticamente convertido para 0
if (fopen_s(&f, nomeFich, "wb") == NULL)
    /* fopen_s funcionou */
else
{
    /* fopen_s não funcionou */
}

Note: in my view it is better to use 0 in comparison; there is no advantage in "putting NULL at the noise"

  • Thank you, I thought the safe shape of the fopen works the same way as the fopen. But even so, I cannot write the structure in file, just write me a character : 'è' , nothing more

0

aux is a pointer.

fwrite(&aux, sizeof(SuperDume) * CONJUNTOSUPERDUME,1,f);
//     |     ^^^^^^^^^^^^^^^^^ tamanho dum ponteiro
//     \--> endereço dum ponteiro

Try

if (fwrite(aux, sizeof *aux, CONJUNTOSUPERDUME, f) != CONJUNTOSUPERDUME) /* erro */;
//              \---------/  ^^^^^^^^^^^^^^^^^ numero de elementos
//                tamanho de cada elemento

But don’t put the fwrite within a cycle; or else put, but with only 1 element at a time.

  • I already tried to write Superdume (data type consisting of two hash Tables and one "string") But I couldn’t get anything, I asked a teacher of another programming language and he told me to use serialize. I looked around but I didn’t understand any of it. .

Browser other questions tagged

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