0

I have the following structure and I am trying to write/read it to/from a file.

struct block{
    int id;
    int row, col;
    int alive;

    struct block *next_up, *next_dn, *next_lt, *next_rt;
};

I have the following read and write functions, but they are returning the wrong address, possibly the address of the pointer, not the address pointed at. The pointers will either be NULL or contain the memory address of a struct block.

save:

void save_blocks(){
    FILE *my_stream;
    my_stream = fopen ("blocks.txt", "w");

    int c;
    for(c=0;c<blk_cnt;c++){

        fprintf(my_stream, "%d ", the_blocks[c].id);
        fprintf(my_stream, "%d ", the_blocks[c].row);
        fprintf(my_stream, "%d ", the_blocks[c].col);
        fprintf(my_stream, "%d ", the_blocks[c].alive);

        fprintf(my_stream, "%p ", the_blocks[c].next_up);
        fprintf(my_stream, "%p ", the_blocks[c].next_dn);
        fprintf(my_stream, "%p ", the_blocks[c].next_lt);
        fprintf(my_stream, "%p ", the_blocks[c].next_rt);

    }
    fflush (my_stream);
        fclose (my_stream);
}

this is the load:

void load_blocks(){
    blk_cnt = 0;

    int id, row, col, alive;
    struct block *next_up, *next_dn, *next_lt, *next_rt;

    FILE *my_stream;
    my_stream = fopen("blocks.txt", "r");

    while(!feof(my_stream)){
        int blk_cnt = fscanf(my_stream, "%d %d %d %d %p %p %p %p",
            &col, &alive, &next_up, &next_dn, &next_lt, &next_rt);

        the_blocks[blk_cnt].id = id;
        the_blocks[blk_cnt].row = row;
        the_blocks[blk_cnt].col = col;
        the_blocks[blk_cnt].alive = alive;

        the_blocks[blk_cnt].next_up = next_up;
        the_blocks[blk_cnt].next_dn = next_dn;
        the_blocks[blk_cnt].next_lt = next_lt;
        the_blocks[blk_cnt].next_rt = next_rt;
        blk_cnt++;
    }
}

I tried using fwrite and fread but it was giving the wrong addresses for the pointers. I feel like I need to write &(*next_up) but this seems wrong. I have searched and this question is similar, but not quite what I need. Finally, do I actually need the while(!feof()) loop? TIA.

Community
  • 1
  • 1
marienbad
  • 1,461
  • 1
  • 9
  • 19
  • 2
    The problem is that you can't really save/load pointers, as two runs of your program will not put things at the same places in memory. Not to mention that your loading doesn't actually allocate any memory for those pointers. You might want to read about [marshalling](http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29) and [serialization](http://en.wikipedia.org/wiki/Serialization). – Some programmer dude Mar 26 '14 at 09:59
  • Thanks mate, I (stupidly) hadn't thought about that. I changed it so it only saves id/row/col and links up when they are reloaded. Thanks again. – marienbad Mar 26 '14 at 14:06

1 Answers1

0

Instead of saving the pointers, give each block a unique identifier (is that what id is) and save that instead. Then after you've loaded the complete array, loop through every element and replace the unique id with the address of the corresponding block.

Anonymouse
  • 935
  • 9
  • 20
  • This is such a brill and simple idea I can't believe I didn't think of it, although I am trying to learn pointers so had a bit of tunnel vision going on. Will try to think things through better in future, but thanks again. – marienbad Mar 26 '14 at 14:07
  • No problem, now if you'd be so kind as to vote my answer UP, that would be totally cool. – Anonymouse Mar 26 '14 at 14:42
  • I don't have enough points to vote up, sorry; I am proper new here. – marienbad Mar 27 '14 at 11:59