I'm currently working on a program that uses these two structs:
//Struct to hold contact info about a friend
typedef struct
{
char *firstName, *lastName;
char *home, *cell;
} Friend;
//Struct to hold a list of friends and keep track of its size.
typedef struct
{
Friend *listEntries;
size_t listSize;
} FriendList;
Each of the string members within the Friend struct is dynamically allocated, as is the listEntries array inside FriendList. I'm trying to save a FriendList as a binary record, but when I try to do this using fwrite() and then read it back with fread(), the listEntries array of the FriendList that is read from the file is empty. Is there any way to save a dynamic struct like this?
EDIT: The functions I use to read/write the struct are:
void writeList(FriendList *fl, char* filename)
{
FILE *fp = fopen(filename, "wb+");
if(fp != NULL)
{
fwrite(fl, sizeof(FriendList), 1, fp);
}
fclose(fp);
}
void readList(FriendList *dest, char* filename)
{
FILE *fp = fopen(filename, "rb");
if(fp == NULL)
{
printf("oops...");
}
else
{
fread(dest, sizeof(FriendList), 1, fp);
}
close(fp);
}