Dulguuntur
Dulguuntur

Reputation: 51

C writing Linked List into file

I am trying to write linked list to file. I have a problem when adding new element. Main problem is when assigning struct (read from file) to new_node->next_node, only pointer saved into new_node->next_node. After i read from the file, try to print then first struct value was printed well and then second value (node_trx->next_node) was printed wrong. I need to store values not pointer to node_trx->next_node. So is there anyone who can tell what did i wrong?

Here is my struct:

typedef struct {
    char amount[12];
    int currency;
    struct node_trx * next_node;
} node_trx;

Here is add new element code:

 node_trx * trx_list;
 if (3 != read_last_trx_from_file(EMVP_LAST_TRANSACTION_OBJECT, &trx_list, &data_set_len)) {
    if (NULL == (f = fopen(tools_get_full_filename_const(EMVP_LAST_TRANSACTION_OBJECT), "w+"))) {
        log_ntrx_error(-1, "Cannot open ticket data set file: %s.",
                strerror(errno));
    } else {
        node_trx * new_node = calloc(1, sizeof(node_trx));

        strcpy(new_node->amount, disp_amount);

        new_node->currency = currency_code;

        memcpy(&new_node->next_node, &trx_list, sizeof trx_list);

        if (1 != fwrite(new_node, data_set_len + sizeof(node_trx), 1, f)) {
            //error
        }

        free(new_node);
        free(trx_list);
        fclose(f);
    }
} else {
    // Saving first element
    if (NULL == (f = fopen(tools_get_full_filename_const(EMVP_LAST_TRANSACTION_OBJECT), "w")))
    {
        log_ntrx_error(-1, "Cannot open ticket data set file: %s.",
                strerror(errno));
    } else {
        trx_list = BKS_XCALLOC(1, sizeof(node_trx));

        strcpy(trx_list->amount, disp_amount);

        trx_list->currency = currency_code;
        trx_list->next_node = NULL;

        if (1 != fwrite(trx_list, sizeof(node_trx), 1, f)) {

        }

        fclose(f);
    }

Here is file reading function:

int read_last_trx_from_file (const char * file_name, node_trx * * trx, unsigned * trx_len)
{
    FILE * f;
    *trx = NULL;

    if (NULL == (f = fopen(tools_get_full_filename_const(file_name), "rb")))
    {

    }

    size_t fsize;
    fseek(f, 0, SEEK_END);
    fsize = ftell(f);
    fprintf(stdout, "file size: %zd\n", fsize);
    if (!fsize)
    {
        return 3; // No data
    } else {
        if (fsize == 1) {
            return 3; // No data
        }
    }

    rewind(f);
    if (NULL != (*trx = (node_trx *) BKS_XCALLOC(1, fsize)))
    {
        if (1 != fread(*trx, fsize, 1, f))
        {
            fclose(f);
            return 2;
        }
    }
    fclose(f);

    *trx_len = fsize;
    return 0;
}

Here is my printing data from file code:

 node_trx * card_data;
 if (3 != read_last_trx_from_file(LAST_TRX_OBJECT, &card_data, &data_set_len)) {
 while (card_data->next_node != NULL) {
     strcpy(amount, card_data->amount);
     printf("%s AMOUNT \n", amount);
     printf("%d CURRENCY \n", card_data->currency);
     card_data = card_data->next_node;
 }
}

Upvotes: 0

Views: 373

Answers (1)

Lanting
Lanting

Reputation: 3068

If the linked list is sequential, you could just store the elements in order they occur.

Upvotes: 0

Related Questions