Below is the structure defined.
typedef struct{
int a;
char b;
}X;
typedef struct{
X m;
int c;
char d;
}B;
B n,q;
n.m.a = 12;
n.m.b = 'a';
n.c = 13;
n.d = 'b';
I do a fwrite of the following structure in a file. File is opened as below.
fp = fopen("D://tests//t11test.txt","wb");
fwrite(&n, sizeof(B), 1, fp);
The fwrite is successful and I checked the contents of the file corresponding to fp.
But when I do a fread on the same file after closing and reopening the file, I am not able to read the contents of the sub-structure m. The fread is
fp = fopen("D://tests//t11test.txt","rb");
fread(&q, sizeof(B), 1,fp);
Where am I going wrong?
I can’t see what the problem is, but, FWIW, this worked:
Output: