>How would I get around this?
You have to do your own deep copy by either writing each value individually, or packing the structure into some form that can be retrieved easily:
Code:
#include <stdio.h>
#include <stdlib.h>
struct test {
int a;
int b;
int *c;
};
static int pack_buf[4];
void pack(struct test *st)
{
pack_buf[0] = st->a;
pack_buf[1] = st->b;
pack_buf[2] = st->c[0];
pack_buf[3] = st->c[1];
}
void unpack(struct test *st)
{
st->a = pack_buf[0];
st->b = pack_buf[1];
st->c[0] = pack_buf[2];
st->c[1] = pack_buf[3];
}
void test_write(const char *filename)
{
FILE *out = fopen(filename, "wb");
struct test t = {123, 456, NULL};
t.c = malloc(2 * sizeof *t.c);
t.c[0] = 10;
t.c[1] = 20;
pack(&t);
fwrite(pack_buf, sizeof(int), 4, out);
fclose(out);
}
void test_read(const char *filename)
{
FILE *in = fopen(filename, "rb");
struct test t;
t.c = malloc(2 * sizeof *t.c);
fread(pack_buf, sizeof(int), 4, in);
unpack(&t);
printf("%d -- %d -- [%d,%d]\n", t.a, t.b, t.c[0], t.c[1]);
fclose(in);
}
int main(void)
{
test_write("file.txt");
test_read("file.txt");
return 0;
}