Questo è un esempio, ma bada bene che c'è un errore dentro... studialo e trova l'errore
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
char a;
int b;
struct node *next;
}node;
node *ins_node (node **head, node *mynode)
{
node *nuovo;
if ((nuovo=malloc(sizeof(node)))==NULL)
return NULL;
memcpy(nuovo,mynode,sizeof(node));
nuovo->next=*head;
*head=nuovo;
return nuovo;
}
void freeall (node **head)
{
node *tmp,*next;
for (tmp=*head;tmp;tmp=next)
{
next=tmp->next;
free(tmp);
}
*head=NULL;
}
void dspy (node *head)
{
node *tmp;
for (tmp=head;tmp;tmp=tmp->next)
printf ("%c %d\n",tmp->a,tmp->b);
}
void save (node *head,FILE *stream)
{
node *tmp;
fseek (stream,0L,SEEK_SET);
for (tmp=head;tmp;tmp=tmp->next)
fwrite (tmp,sizeof(node),1,stream);
}
void load (node **head,FILE *stream)
{
node tmp;
size_t n;
fseek (stream,0L,SEEK_SET);
while (!feof(stream))
{
if ((n=fread (&tmp,sizeof(node),1,stream)))
ins_node (head, &tmp);
}
}
int main ()
{
FILE *stream=fopen("foobar.txt","wb+");
node *head=NULL,foo;
if (!stream)
return -1;
/** example */
foo.a='A'; foo.b=10 ; ins_node (&head, &foo);
foo.a='B'; foo.b=20 ; ins_node (&head, &foo);
foo.a='C'; foo.b=30 ; ins_node (&head, &foo);
/** test it */
save (head,stream);
freeall (&head);
load (&head,stream);
dspy(head);
freeall (&head);
fclose (stream);
return 0;
}
edit: corretto dagli errori di distrazione... rimane lo stesso errore quale?