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??

Recommended Answers

All 8 Replies

How did you open it. Can you post the code

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

}

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

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.

I'm sorry .I was in such a hurry and I forgot .sorry

could anyone define the problem in my code .please!!

sam511,

I think you tried to open a data file with an editor. There are two types of data file - Text and Binary.
Show us your complete code. Don't forget; wrap up source code with BB code tags.

Another post ?

http://www.daniweb.com/forums/thread200229.html

commented: Dunno, maybe; the OP has spammed so many threads that I lost interest. +36
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.