Hello
We are studying C at college in sweden, just started last weekand are stuck at this with linked lists.
(Working in VC++)
Using a struct looking like this(public declared)
first we want to add item to the end of the list thru a function declared like this:Code:struct stud { char kursid[10]; char pnr[12]; char name[30] int test_score; struct stud *spek; }; struct stud *rot,*ny; FILE *fp;
int laggtill(struct stud **studpek);
We have managed to add items to the front of the list but we can not add the new items to the end of the list.
"visually" we have achieved this:
Root->item3->item2->item1
but we want it to look like this:
root->item1->item2->item3
That was the first part of our problem.
We have succeded to save the struct to a file binary thru this code:
How do we read the file? we have tried to use fread thru this code:Code:void savetofile(struct stud *studskriv) while(studskriv!=NULL) { fwrite(studscriv,sizeof(struct stud),1,fp); studskriv=studskriv->spek; }
This adds item to the "front" of the list, as in the first Q, How do we add to the end of the list?Code:void readfromfile(struct stud **studpek) { int numread; struct stud *new; //open file //check for error in open //allocate memory for the struct while((foef(fp))==0) { new=(struc stud *)malloc(sizeof(struct stud)); //check for malloc error new->spek=*studpek; *studpek=new; numread=fread(*studpek,sizeof(struct stud),1,fp); } }//end function
Any help apriciated, we have been stuck with this for 4 days now and are getting desperate.
Thank you
Anders & Kent




Reply With Quote