0

I'm trying to read data from a file, a bunch of ints, two 2d arrays and two 1d arrays, the ints read just fine, but the arrays seem to read the wrong data, I'm not sure where the problem is: the writing, the reading or memory allocation. I'm not getting any compiler errors or crashes, but I can't find the issue. I'd apreciate it if you could point it out.

void save(int found, int q, int a, int b, int dir,int **board,int **x_board, int n_players, int *p_ships, int *e_ships,int n_ships, int hor, int ver)
{
    int data[]={found,q,a,b,dir,p_ship_count,e_ship_count,n_players,n_ships,hor,ver};
    FILE *sfp;
    if((sfp = fopen("savegame","w"))==NULL)
            {
                 printf("Failed to open file");
                 exit(0);
            }
    fwrite(data,sizeof(int),sizeof(data),sfp);
    fwrite(p_ships,sizeof(int),sizeof(p_ships),sfp);
    fwrite(e_ships,sizeof(int),sizeof(e_ships),sfp);
    fwrite(board,sizeof(int),sizeof(board),sfp);
    fwrite(x_board,sizeof(int),sizeof(x_board),sfp);
    fclose(sfp);
}

int main()
{
...
int **a_board;
int **b_board;
int *p_ships;
int *e_ships;
...
            FILE *rfp;
            if((rfp = fopen("savegame","r"))==NULL)
            {
                 printf("Failed to open file");
                 exit(0);
            }
            else
            {
                fread(&found, sizeof(int), 1, rfp);
                fread(&q, sizeof(int), 1, rfp);
                fread(&a, sizeof(int), 1, rfp);
                fread(&b, sizeof(int), 1, rfp);
                fread(&dir, sizeof(int), 1, rfp);
                fread(&p_ship_count, sizeof(int), 1, rfp);
                fread(&e_ship_count, sizeof(int), 1, rfp);
                fread(&n_players, sizeof(int), 1, rfp);
                fread(&n_ships, sizeof(int), 1, rfp);
                fread(&hor, sizeof(int), 1, rfp);
                fread(&ver, sizeof(int), 1, rfp);

                p_ships=(int*)calloc(4*n_ships,sizeof(int));
                e_ships=(int*)calloc(4*n_ships,sizeof(int));
                fread(p_ships,sizeof(int),n_ships*4,rfp);
                fread(e_ships,sizeof(int),n_ships*4,rfp);

                a_board = (int**)calloc(hor+2,sizeof(int*));
                for(i=0;i<hor+2;++i)
                {
                    a_board[i]=(int*)calloc(ver+2,sizeof(int));
                }
                b_board = (int**)calloc(hor+2,sizeof(int*));
                for(i=0;i<hor+2;++i)
                {
                    b_board[i]=(int*)calloc(ver+2,sizeof(int));
                }
                if(n_players==1 || n_players==0)
                {
                    fread(*b_board,sizeof(int),(hor+2)*(ver+2),rfp);
                    fread(*a_board,sizeof(int),(hor+2)*(ver+2),rfp);
                }
                if(n_players==2)
                {
                    fread(*a_board,sizeof(int),(hor+2)*(ver+2),rfp);
                    fread(*b_board,sizeof(int),(hor+2)*(ver+2),rfp);
                }

                fclose(rfp);
            }
Jan Kager
  • 1
  • 1
  • `fwrite(p_ships,sizeof(int),sizeof(p_ships),sfp);` - this writes either 16, 32 or 64 bytes (depending on your platform). Is that what you intended? Since `sizeof(int)` is the size of an `int` and `sizeof(p_ships)` is the size of a pointer, it will write (size of `int`) * (size of pointer) bytes. – user253751 Jun 03 '16 at 11:33
  • No, it's supposed to be the number of records in the array, I'll change it now... fwrite(p_ships,sizeof(int),4*n_ships,sfp); - now it crashes :(, – Jan Kager Jun 03 '16 at 11:41
  • Possible duplicate of [How to find the 'sizeof'(a pointer pointing to an array)?](http://stackoverflow.com/questions/492384/how-to-find-the-sizeofa-pointer-pointing-to-an-array) – Andrew Henle Jun 03 '16 at 11:42
  • To make it clear - given `int *p_ships`, `sizeof( p_ships )` returns the size of the *pointer*, not what it points to. `sizeof()` is a *compile-time* result. – Andrew Henle Jun 03 '16 at 11:44
  • Please narrow the problem down and post an [MCVE](http://stackoverflow.com/help/mcve). – Jabberwocky Jun 03 '16 at 11:46
  • @JanKager So now it writes (size of `int`) * 4 * n_ships bytes. fwrite multiplies the middle two arguments, then writes that many bytes. – user253751 Jun 03 '16 at 23:55

0 Answers0