I want to write the data that the linked list holds to disk. I believe the function that writes the data is code correctly. Its reading that I have problems with. I take my problem is that headRef is local. How would I get around this?
Code:
struct node {
int data;
struct node* next;
};
int saveList(struct node** headRef, char* filename)
{
struct node* current = *headRef;
FILE *fp;
if(current == NULL)
return 1;
fp = fopen(filename,"wb");
if(fp == NULL)
return 2;
while(current != NULL)
{
fwrite(¤t->data, sizeof(struct node), 1 , fp);
current = current->next;
}
fclose(fp);
return 0;
}
/**********************************************************/
int loadList(struct node** headRef, char* filename)
{
struct node* current = *headRef;
int data;
FILE *fp = fopen(filename,"rb");
if(fp == NULL)
return 1;
while(fread(&data, sizeof(struct node), 1, fp) != 0)
{
addToList(¤t,data);
}
fclose(fp);
return 0;
}