onerrorgoto
Feb 14th, 2001, 02:43 AM
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)
struct stud
{
char kursid[10];
char pnr[12];
char name[30]
int test_score;
struct stud *spek;
};
struct stud *rot,*ny;
FILE *fp;
first we want to add item to the end of the list thru a function declared like this:
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:
void savetofile(struct stud *studskriv)
while(studskriv!=NULL)
{
fwrite(studscriv,sizeof(struct stud),1,fp);
studskriv=studskriv->spek;
}
How do we read the file? we have tried to use fread thru this 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
This adds item to the "front" of the list, as in the first Q, How do we add to the end of the list?
Any help apriciated, we have been stuck with this for 4 days now and are getting desperate.
Thank you
Anders & Kent
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)
struct stud
{
char kursid[10];
char pnr[12];
char name[30]
int test_score;
struct stud *spek;
};
struct stud *rot,*ny;
FILE *fp;
first we want to add item to the end of the list thru a function declared like this:
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:
void savetofile(struct stud *studskriv)
while(studskriv!=NULL)
{
fwrite(studscriv,sizeof(struct stud),1,fp);
studskriv=studskriv->spek;
}
How do we read the file? we have tried to use fread thru this 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
This adds item to the "front" of the list, as in the first Q, How do we add to the end of the list?
Any help apriciated, we have been stuck with this for 4 days now and are getting desperate.
Thank you
Anders & Kent