hi , I'm trying to store linked list in a file . I opened a file with extension txt but when I opened it to read it . It was encrypted
does anyone know why??

Thanks It opened when I put run again I have another problem I'm using two linked lists and I opened two files for each one but when I write list to file It writes both lists in the two files .

here is the code

void save (school *c,school *m)
{




      fpta=fopen("school.txt", "w");

      fptb=fopen("school1.txt", "w");

       c=head;
       m=head;

       while(c!=NULL)
      { 
           fprintf(fpta,"\n%d\n%s\n%s\n%d\n%s\n%d\n%f",c->id,c->name,c->address,
          c->phone,c->sex,c->level,c->average);
         c=c->next;
      }

      fclose(fpta);

      printf("\nFile one has been saved");


      while (m!=NULL)

      { 
          fprintf(fptb,"\n%d\n%s\n%s\n%d\n%s\n%d\n%f",m->id,m->name,m->address,
          m->phone,m->sex,m->level,m->average);

          m=m->next;

      }

        fclose(fptb);

       printf("\nFile two has been saved");


getch();

}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

>I have another problem I'm using two linked lists and I opened two files for each one but when I write list to file It writes both lists in the two files .

You are using c and m as two pointers to mark the two lists right? Thats the reason you are passing them.But why this?

c=head;
m=head;

by this you are making both c and m point to head so whats the use of passing them to the function ???

Well you have opened your files fpta and fptb in mode "w" write mode and so it would delete all the contents of it next time you open it and write fresh so have a look at it.

I used a condition according to level number and it worked .but I think the problem is with add function .I'll post the structure and add function .If anyone could correct it.
here is the code

typedef struct school {
      int id;
      char name[80]; 
      char address[80];
      int phone;
      char sex[10];
      int level;
      float average;
      struct school *next;


}school;

 struct school*last=NULL,*head=NULL;
 struct school *student1;//pointer to list one
 struct school *student2;//pointer to list two

void add(struct school *one)
{
    /*struct*/ 
        one = (school *) malloc (sizeof(school));
        printf("\n\t\t Add The Information To system " );
        printf("\n\t\t\tID : ");
        scanf(" %d", &one->id);


        printf("\t\t\tNAME : ");
        scanf("%s" , one->name);


        printf("\t\t\tAddress : ");
        scanf(" %s",one->address);


        printf("\t\t\tphone : ");
        scanf("%d" , &one->phone);


        printf("\t\t\tsex : ");
        scanf("%s" , one->sex);


        printf("\t\t\tlevel : ");
        scanf("%d" , &one->level);


        printf("\t\t\taverage : ");
        scanf("%f" , &one->average);


         one->next = NULL;


      if (head == NULL)
      {
       last = one;
       head = one;
       }
       else
       {
        last->next = one;
        last = one;
        }



         printf("\n\n******************************");

         return;
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

I'd say your first problem is your inability to use code tags, leading to nobody caring enough to waste their time to read your code.

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