Write list in binary file

Asked

Viewed 149 times

4

The idea of this method is to write all the words of a list of nodes in a binary file. I have a linked list of nodes, where each node has two information, its information, and a reference to the next node in the list. The idea is to write the information of each node in a binary file. However, I am getting the following error:

Warning: incompatible Pointer to integer Conversion assigning to 'char' from 'const char *'

Which occurs on this line of the code:

*pal = nodo->info;

I just don’t understand what the problem is, since my structure defines the information of the node as a pointer as well. Below is the NODO structure code and method

typedef struct Temp{
    char const *info;
    struct Temp *prox;
}TNODOPAL;

Method :

void escreveBinario(){

    TNODOPAL *nodo = inicioPal;

    FILE *fp;
    char *pal = NULL;

    fp = fopen( "binary.dat" , "wb" );

    if (fp == NULL) // Se não conseguiu criar
    {
        printf("Problemas na CRIACAO do arquivo\n");
        return;
    }

    while(nodo != NULL){
        *pal = nodo->info;
        fwrite (&pal, sizeof(char) * 30, 200, fp);
        nodo = nodo->prox;
    }
    fclose(fp);

}

1 answer

3


pal is a pointer to char then *pal will be a memory position containing a char. When você faz*pal = cvocê está armazenando um char na posição de memória apontada pelo ponteiropal`.

I think what you wanted to do is

pal = nodo->info
fwrite (pal, sizeof(char) * 30, 200, fp);

Or simply

fwrite (nodo->info, sizeof(char) * 30, 200, fp);
  • I just noticed that the created file is empty. You know what the possible problem might be?

Browser other questions tagged

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