I have the following structs:
typedef struct {
char last[NAMESIZE];
char first[NAMESIZE];
} name;
typedef struct {
int id;
name name;
float score;
} record;
typedef struct {
record *data;
size_t nalloc;
size_t nused;
} record_list;
I am trying to read records from a file using this function:
int file_read(record_list *list, record* rec)
{
/* irrelevant code eliminated */
while(fread(&records, sizeof(record), 1, fp))
{
*rec = records;
valid_list_insert = list_insert(list, rec);
if(!valid_list_insert)
{
return 0;
}
}
}
and I am trying to write record_list to a file using this function:
int file_write(record_list* list)
{
/* irrelevant code eliminated */
if(fwrite(&list, sizeof(record_list), 1, fp) == 0)
{
return 0;
}
}
However, neither of them work correctly. fread reads in data at, what appears to be, random when I try to display a record_list in my program. And fwrite just writes random characters into a file. Any help on why this isn't working would be greatly appreciated.