hi i am doing code on single linked list in that i hav to save elements of my list to a file and then load it. The file is not loading pls check this code
#include<stdio.h>
#include<stdlib.h>
struct llist
{
int info;
struct llist *next;
};
typedef struct llist linked_list;
void ll_insert(linked_list **start);
void ll_delete(linked_list **start);
linked_list * createnode();
linked_list * addnode(linked_list **start, linked_list *new);
void ll_traverse(linked_list *start);
void ll_save(linked_list *start);
void ll_load(linked_list **start);
linked_list *createnode1(int *c1);
int main()
{
linked_list *start=NULL;
char menu;
do
{
printf("1.Insertion\n");
printf("2.deletion\n");
printf("3.Traverse\n");
printf("4.Save\n");
printf("5.Load\n");
printf("6.Exit\n");
menu=getchar();
switch(menu)
{
case '1': ll_insert(&start);
break;
case '2': ll_delete(&start);
break;
case '3': ll_traverse(start);
break;
case '4': ll_save(start);
break;
/* case '5':ll_saveas(start);
break;*/
case '5': ll_load(&start);
break;
case '6': return;
}
}while(menu!='6');
return 0;
}
linked_list *createnode1(int *c1)
{
linked_list *new1;int c,info;
c=(int)*c1;
new1=(linked_list *)malloc(sizeof(linked_list));
new1->info=c;
new1->next=NULL;
return new1;
}
void ll_insert(linked_list **start)
{
char ch;
linked_list *new;
new=createnode();
if(*start==NULL)
*start=new;
else
addnode(start,new);
}
linked_list *createnode()
{
int info;
linked_list *new;
new=(linked_list *)malloc(sizeof(linked_list));
printf("Enter the info: ");
scanf("%d",&new->info);
new->next=NULL;
return new;
}
linked_list *addnode(linked_list **start,linked_list *new)
{
linked_list *prev,*ptr;
linked_list *tempStart;
tempStart=*start;
if(new->info < tempStart->info)
{
new->next=tempStart;
*start=new;
return;
}
for(prev=tempStart,ptr=tempStart->next;ptr;prev=ptr,ptr=ptr->next)
{
if(new->info<ptr->info)
{
prev->next=new;
new->next=ptr;
return ;
}
}
if(ptr==NULL)
{
prev->next=new;
return ;
}
return;
}
void ll_delete(linked_list **start)
{
linked_list *ptr,*prev,*temp;
linked_list *tempStart;
int i;
printf("Enter the node to be deleted:");
scanf("%d",&i);
tempStart = *start;
if(tempStart->info==i)
{
temp=tempStart;
*start= tempStart->next;
free(temp);
return;
}
for(prev=tempStart,ptr=tempStart->next;ptr;prev=ptr,ptr=ptr->next)
{
prev->next=ptr->next;
free(ptr);
return ;
}
if(ptr==NULL)
printf("Element not found");
}
void ll_traverse(linked_list *start)
{
linked_list *ptr;
if(&start==NULL)
printf("Linked list is empty");
else
{
ptr=start;
while(ptr)
{
printf("%d\n",ptr->info);
ptr=ptr->next;
}
}
}
void ll_save(linked_list *start)
{
linked_list *ptr;
char fname[20];
printf("Enter the file to be saved");
scanf("%s",fname);
FILE *fp;
int c;
fp=fopen(fname,"w");
ptr=start;
while(ptr)
{
c=ptr->info;
fprintf(fp,"%d ",c);
ptr=ptr->next;
}
}
void ll_load(linked_list **start)
{
char fname[10];
linked_list *new1;
FILE *fp1;
int c1;
printf("\nEnter which file to be loaded\n");
scanf("%s",fname);
fp1=fopen(fname,"r");
c1=fgetc(fp1);
while(c1!=EOF)
{
linked_list *new1;
new1=createnode1(&c1);
if(*start==NULL)
*start=new1;
else
addnode(start,new1);
c1=fgetc(fp1);
}
}