1

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.

bitmask
  • 32,434
  • 14
  • 99
  • 159
LoveThatPink
  • 19
  • 1
  • 3
  • 2
    If you believe the problem to be with `fread`/`fwrite`, the first thing to do is eliminate all the irrelevant code, to make your life easier, and to make your question easier for someone to answer. So get rid of all the stuff to do with parsing the user input! – Oliver Charlesworth Aug 05 '11 at 00:11
  • 3
    Make `gdb` your friend. One time investment (learning curve) which you will reap over and over your entire life! – hari Aug 05 '11 at 00:12

1 Answers1

6

Well, one thing that will not work is writing pointers (as in the data member of the record_list): because (1) it will write the value of the pointer (i.e. the numeric value of some spot in memory) and (b) you won't allocate your stuff in the same place when you read it back in again. Instead, you will have to carefully parse the structure and record the data in a way that you can get it back again.

This process is called "serialization" (and the reverse is "deserialization"). See also Serialize Data Structures in C.

Community
  • 1
  • 1
dmckee --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234
  • I've been looking at examples of fread and fwrite, and they all appear to be using the function the same way as I am. I am still very new to C, do you think you could give me an example of using serialization in the context of what I am trying to do? – LoveThatPink Aug 05 '11 at 01:45
  • 1
    Start by seeing if you can simply use `fread`/`fwrite` on something like `typedef struct { int i; char s[8] } dumbstruct;`. If that works think about how would would serialize a linked list of `dumbstruct`s: `typedef struct node_t { node_t*next; dumbstruct payload; } node;`. The critical question here is *"How do I represent the **structure** (the list-ness which is encoded in the pointers) as well as the **data**?"* – dmckee --- ex-moderator kitten Aug 05 '11 at 01:52