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);
  }
}

Edited 6 Years Ago by WaltP: Added CODE Tags

In your load function you call fgetc, that gets a single character from the file so for a number like say 10 it returns '1' which has the value 49. You then make a node out of the value 49 and continue making nodes out of every character in the file.

You need to read the whole number into an array of characters that is every digit until the next space which is what you have used as a separator. Actually using space as a separator makes things a little difficult, if you used newline '\n' as a separator then you could read the file something like this

char text[100];
int value;
int closeFile;

closeFile = (fgets(text, sizeof text, fpl) == NULL);
if (!closeFile)
{
    value = atoi(text);
    // Insert into list
}

IF your looking at parsing a list of single elements you could try something simpler along the following lines cause it uses the split function to deciper an input string based on a separator that you supply the function:

#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<alloc.h>

struct node* split(char splitc,char *astring);

//-------------------------------------------------
struct node
{
  char data[255];
  struct node *next;
};
//------------------------------------------------------------
//------------------------------------------------------------------
//----------------------------------------------------
void main()
{
   char astring[255] = { "hello this is a text message" };
   struct node *current,*start;

   //Decode the astring with the split funtion using space separator
	start = split(' ',astring);

   //display

   printf("\nThe Linked List : \n");
   current=start;
   while(current!=NULL)
   {
   printf("&current->data:%s\n",&current->data);
   current=current->next;

   }
 	printf("\nPress any key to continue...");
 	getch();
}
//----------------------------------------------------

struct node* split(char splitc,char *astring){

	int d=0;
   int i=0;
   int c=0;
   int e=0;
   int f=0;

   char opcstr[255];
   struct node *new_node,*current,*list;
   f=0;
 	for(i=0;i<=strlen(astring);i++)
   {
     if(i<=strlen(astring))
     {
   	if((astring[i] == splitc)||(i==strlen(astring))) {
      opcstr[c] = NULL;
      printf("OPCSTR:%s\n",opcstr);
         new_node=(struct node *)malloc(sizeof(struct node));
         if (f==0) {
         	list = NULL;
         }

         f++;
      	strcpy(&new_node->data[0],opcstr);

   		new_node->next=NULL;

   		if(list==NULL)
   		{
   		list=new_node;
   		current=new_node;
   		}
   		else
   		{
   		current->next=new_node;
   		current=new_node;
   		}
         e=strlen(opcstr);
         for(d=0;d<=e;d++)
         	opcstr[d] = NULL;
         c=0;
      } else {
       	opcstr[c] = astring[i];
      	c++;
      }
     }
   }

   return list;
}

Edited 6 Years Ago by technology: n/a

This article has been dead for over six months. Start a new discussion instead.