save a data type to a file in c language

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 SuperDume to file and I can not:

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, but can not do fwrite , returning me the following error:

Edited.Ihavethefollowingcode

voidEscreveFicheiroCartoes(charnomeFich[]){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); 
}
    
asked by anonymous 09.05.2015 / 03:58

2 answers

1

I can not find the description of fopen_s in the Standard of C; you should be using a non-standard compiler: -)

description of fopen_s provided by Microsoft and how you use the return value by the function are not coherent ... so I assume that is not the version you are using either.

See how your fopen_s works in your compiler manual.

Or use the Standard function that has already been tested over so many years

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

Edit: I found the fopen_s on Standard C11 , on 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 it 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: I think it's best to use 0 in the comparison; there is no advantage in "getting NULL to noise"

    
09.05.2015 / 09:30
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 do not put fwrite inside a cycle; or else, but with only one element at a time.

    
10.05.2015 / 09:15