EDIT: This can be done by a single execution of fwrite/fread to the nested pointer to struct, as follows:
//write
Path_pointer c;
c.point = (Point*) calloc(2, sizeof(Point));
c.point[0] = (Point){3, 5};
c.point[1] = (Point){-2, 7};
FILE * f = fopen("binary.txt", "wb");
if(f == NULL) exit(1);
fwrite(c.point, 2*sizeof(Point), 1, f);
fclose(f);
//read
Path_pointer d;
FILE * f = fopen("binary.txt", "rb");
if (f == NULL) exit(1);
d.point = (Point*) calloc(2, sizeof(Point));
fread(d.point, 2*sizeof(Point), 1, f);
printf("point[0]=(%d,%d)\n", d.point[0].x, d.point[0].y);
printf("point[0]=(%d,%d)\n", d.point[1].x, d.point[1].y);
fclose(f);
I have posted this question because it is different from using pointers in structures to read from file and Writing and reading (fwrite - fread) structures with pointers since:
it includes dynamic memory allocation (and in this case the question appears what to put as the second parameter of fwrite and fread).
the aforementioned questions did not receive a proper answer with fwrite() and fread() functions.
A way to write (using fwrite) to a binary file a struct with a pointer to struct inside, and to read it (using fread) is to do it element by element. My main question is how to write/read such a struct by a single execution of fwrite/fread. This answer cannot be found anywhere in stackoverflow, neither in the two aforementioned posts, nor in any other post.
Original question:
The case with array of struct Point works fine (Path_array), whereas the case with pointer to struct Point does not work (Path_pointer). Perhaps the problem is at the second parameter of functions fwrite() and fread() (i.e., "2*sizeof(Point)").
If I want to write/read a struct like Path_pointer to a binary file, which is the proper way to do it, preferably by a single execution of fwrite/fread?
#include<stdio.h>
#include<stdlib.h>
typedef struct {
int x, y;
}Point;
typedef struct {
Point point[2];
}Path_array;
typedef struct {
Point *point;
}Path_pointer;
FILE * f;
int main() {
//--- first case: array of struct Point in Path_array ---
//part 1
Path_array a;
a.point[0] = (Point){3, 5};
a.point[1] = (Point){-2, 7};
f = fopen("binary_array.dat", "wb");
if(f == NULL) exit(1);
fwrite(&a, sizeof(a), 1, f);
fclose(f);
//part 2
Path_array b;
f = fopen("binary_array.dat", "rb");
if (f == NULL) exit(1);
fread(&b, sizeof(b), 1, f);
printf("point[0]=(%d,%d)", b.point[0].x, b.point[0].y);
printf(" and point[1]=(%d,%d)\n",b.point[1].x, b.point[1].y);
fclose(f);
//--- second case: pointer to struct Point in Path_pointer ---
//part 3
Path_pointer c;
c.point = (Point*) calloc(2, sizeof(Point));
c.point[0] = (Point){3, 5};
c.point[1] = (Point){-2, 7};
f = fopen("binary_pointer.dat", "wb");
if(f == NULL) exit(1);
fwrite(&c, 2*sizeof(Point), 1, f);
fclose(f);
//part 4
Path_pointer d;
d.point = (Point*) calloc(2, sizeof(Point));
f = fopen("binary_pointer.dat", "rb");
if (f == NULL) exit(1);
fread(&d, 2*sizeof(Point), 1, f);
printf("point[0]=(%d,%d)", d.point[0].x, d.point[0].y);
printf(" and point[1]=(%d,%d)\n",d.point[1].x, d.point[1].y);
fclose(f);
printf("\n");
return 0;
}