I accepted most of yours advices and this is finally version:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct list
{
int data;
struct list *next;
}LIST;
int MallocCalls=0;
int FreeCalls=0;
void writefile(LIST *);
LIST * readfile();
void Insert(LIST**,LIST *);
void InsertEnd(LIST **,LIST *);
void Free(LIST**);
void Show(LIST*);
size_t Length(LIST *);
int main()
{
LIST *head=NULL,*dummy=NULL;
LIST node;
node.data=1;
Insert(&head,&node);
node.data=2;
Insert(&head,&node);
node.data=3;
Insert(&head,&node);
node.data=4;
Insert(&head,&node);
writefile(head);
Free(&head);
printf("\nAfter Frre() length of list is %d\n",Length(head));
dummy=readfile();
printf("\nAfter reading file length of list is %d\n",Length(dummy));
Show(dummy);
Free(&dummy);
printf ("\nFreeCalls %d, MallocCalls %d\n", FreeCalls, MallocCalls);
return 0;
}
void writefile(LIST *head)
{
FILE *fp;
LIST *dummy;
fp=fopen("List.bin","wb");
if(!fp)
{
printf("\nError opening file!");
exit(1);
}
for(dummy=head;dummy!=NULL;dummy=dummy->next)
if((fwrite(dummy,sizeof(LIST),1,fp))!=1)
{
printf("\nError writing file!");
exit(1);
}
fclose(fp);
}
LIST * readfile()
{
LIST *current,*head=NULL;
FILE *fp;
fp=fopen("List.bin","rb");
if(!fp)
{
printf("\nError opening file!");
exit(1);
}
current=(LIST *)malloc(sizeof(LIST));
if(!current)
{
printf("\nMemory allocation error!");
exit(1);
}
MallocCalls++;
while((fread(current,sizeof(LIST),1,fp))==1)
{
InsertEnd(&head,current);
}
free(current);
FreeCalls++;
return head;
}
void Insert(LIST **head,LIST *node)
{
LIST *current=malloc(sizeof(LIST));
LIST *dummy;
MallocCalls++;
if(!current)
{
printf("Memory allocation error!");
exit(1);
}
memcpy(current,node,sizeof(LIST));
current->next=NULL;
if(!*head)
{
*head=current;
return;
}
dummy=*head;
*head=current;
current->next=dummy;
}
void InsertEnd(LIST **head,LIST *node)
{
LIST*current=*head;
LIST *newnode=malloc(sizeof(LIST));
MallocCalls++;
memcpy(newnode,node,sizeof(LIST));
newnode->next=NULL;
if(!*head)
{
*head=newnode;
return;
}
while(current->next !=NULL)
current=current->next;
current->next=newnode;
}
void Free(LIST**head)
{
LIST *dummy;
while((*head)->next !=NULL)
{
dummy=*head;
*head=dummy->next;
free (dummy);
FreeCalls++;
}
free(*head);
FreeCalls++;
*head=NULL;
}
size_t Length(LIST *head)
{
size_t len=0;
LIST *dummy;
for(dummy=head;dummy!=NULL;dummy=dummy->next)
len++;
return len;
}
void Show(LIST *head)
{
if (!head)
{
printf("List is empty!\n");
return;
}
while (head)
{
printf("%d ", head->data);
head = head->next;
}
}
I'm not satisfied with only one thing: function InsertEnd which I use for restoring list from file
It would be good to write in file in reverse order and then use Insert function that already exists
but then I'll probably have to complicate for loops in writefile and taht job is error prone, so I suppose
this is also good solution.
With global variables for counting malloc and free I discovered memory leak in fuction readfile and added free(current)
Thanks everyone for help!